diff options
Diffstat (limited to 'docs/user-guide/06-interfaces-generics.md')
| -rw-r--r-- | docs/user-guide/06-interfaces-generics.md | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/docs/user-guide/06-interfaces-generics.md b/docs/user-guide/06-interfaces-generics.md index 2436dab07..5b3e89566 100644 --- a/docs/user-guide/06-interfaces-generics.md +++ b/docs/user-guide/06-interfaces-generics.md @@ -94,6 +94,16 @@ Note that it is important to associate a generic type parameter with a type cons While C++ templates are a powerful language mechanism, Slang has followed the path of many other modern programming languages to adopt the more structural and restricted generics feature instead. This enables the Slang compiler to perform type checking early to give more readable error messages, and to speed-up compilation by reusing a lot of work for different instantiations of `myGenericMethod`. +A generic parameter can also be a value. Currently, integer, bool and enum types are allowed as the type for a generic value parameter. Generic value parameters are declared with the `let` keyword. For example: + +```csharp +void g1<let n : int>() { ... } + +enum MyEnum { A, B, C } +void g2<let e : MyEnum>() { ... } + +void g3<let b : bool>() { ... } +``` Supported Constructs in Interface Definitions ----------------------------------------------------- @@ -752,3 +762,39 @@ void test() } ``` This feature is similar to extension traits in Rust. + + +Builtin Interfaces +----------------------------- + +Slang supports the following builtin interfaces: + +- `IComparable`, provides methods for comparing two values of the conforming type. Supported by all basic data types, vector types and matrix types. +- `IRangedValue`, proivides methods for retrieving the minimum and maxinum value expressed by the range of the type. Supported by all integer and floating-point scalar types. +- `IArithmetic`, provides methods for the `+`, '-`, '*`, `/`, `%` and negating operations. Also provide a method for explicit conversion from `int`. Implemented by all builtin integer and floating-point scalar, vector and matrix types. +- `ILogical`, provides methods for all bit operations and logical `and`, `or`, `not` oeprations. Also provide a method for explicit conversion from `int`. Implemented by all builtin integer scalar, vector and matrix types. +- `IInteger`, represents a logical integer that supports both `IArithmetic` and `ILogical` operations. Implemented by all builtin integer scalar types. +- `IDifferentiable`, represents a value that is differentiable. +- `IFloat`, represents a logical float that supports both `IArithmetic`, `ILogical` and `IDifferentiable` operations. Also provides methods to convert to and from `float`. Implemented by all builtin floating-point scalar, vector and matrix types. +- `IArray<T>`, represents a logical array that supports retrieving an element of type `T` from an index. Implemented by array types, vectors and matrices. +- `__EnumType`, implemented by all enum types. +- `__BuiltinIntegerType`, implemented by all integer scalar types. +- `__BuiltinFloatingPointType`, implemented by all floating-point scalar types. +- `__BuiltinArithmeticType`, implemented by all integer and floating-point scalar types. +- `__BuiltinLogicalType`, implemmented by all integer types and the `bool` type. + +Operator overloads are defined for `IArithmetic`, `ILogical`, `IInteger`, `IFloat`, `__BuiltinIntegerType`, `__BuiltinFloatingPointType`, `__BuiltinArithmeticType` and `__BuiltinLogicalType` types, so the following code is valid: + +```csharp +T f<T:IFloat>(T x, T y) +{ + if (x > T(0)) + return x + y; + else + return x - y; +} +void test() +{ + let rs = f(float3(4), float3(5)); // rs = float3(9,9,9) +} +```
\ No newline at end of file |
