diff options
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/user-guide/02-conventional-features.md | 51 | ||||
| -rw-r--r-- | docs/user-guide/06-interfaces-generics.md | 46 | ||||
| -rw-r--r-- | docs/user-guide/toc.html | 1 |
3 files changed, 97 insertions, 1 deletions
diff --git a/docs/user-guide/02-conventional-features.md b/docs/user-guide/02-conventional-features.md index 8c45fec48..be931d5cd 100644 --- a/docs/user-guide/02-conventional-features.md +++ b/docs/user-guide/02-conventional-features.md @@ -160,7 +160,7 @@ enum Channel Unlike C/C++, `enum` types in Slang are always scoped by default (like `enum class` in C++). You can write `enum class` in Slang if it makes you happy, but it isn't required. If you want a `enum` type to be unscoped, you can use the `[UnscopedEnum]` attribute: ```csharp -[[UnscopedEnum] +[UnscopedEnum] enum Channel { Red, Green, Blue @@ -171,6 +171,47 @@ void test(Channel c) } ``` +You can specify an explicit underlying integer type for `enum` types: +```csharp +enum Channel : uint16_t +{ + Red, Green, Blue +} +``` + +By default, the underlying type of an enumeration type is `int`. Enumeration types are implicitly convertible to its underlying type. All enumeration types conform to the builtin `ILogical` interface, which provides operator overloads for bit operations. The following code is allowed: + +```csharp +void test() +{ + Channel c = Channel.Red | Channel.Green; +} +``` + +You can explicitly assign values to each enum case: +```csharp +enum Channel +{ + Red = 5, + Green, // = 6 + Blue // = 7 +} +``` +Slang automatically assigns integer values to enum cases without an explicit value. By default, the value starts from 0 and is increment by 1 for each +enum case. + +You can override the implicit value assignment behavior with the `[Flags]` attribute, which will make value assignment start from 1 and increment by power of 2, making it suitable for enums that represent bit flags. For example: +```csharp +[Flags] +enum Channel +{ + Red, // = 1 + Green, // = 2 + Blue, // = 4 + Alpha, // = 8 +} +``` + ### Opaque Types The Slang standard library defines a large number of _opaque_ types which provide access to objects that are allocated via GPU APIs. @@ -352,6 +393,14 @@ float addSomeThings(int x, float y) } ``` +In addition to the traditional C syntax, you can use the modern syntax to define functions with the `func` keyword: +```swift +func addSomeThings(x : int, y : float) -> float +{ + return x + y; +} +``` + Slang supports overloading of functions based on parameter types. Function parameters may be marked with a _direction_ qualifier: 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 diff --git a/docs/user-guide/toc.html b/docs/user-guide/toc.html index ff64ba74e..487680e98 100644 --- a/docs/user-guide/toc.html +++ b/docs/user-guide/toc.html @@ -78,6 +78,7 @@ <li data-link="interfaces-generics#extending-a-type-with-additional-interface-conformances"><span>Extending a Type with Additional Interface Conformances</span></li> <li data-link="interfaces-generics#is-and-as-operator"><span>`is` and `as` Operator</span></li> <li data-link="interfaces-generics#extensions-to-interfaces"><span>Extensions to Interfaces</span></li> +<li data-link="interfaces-generics#builtin-interfaces"><span>Builtin Interfaces</span></li> </ul> </li> <li data-link="autodiff"><span>Automatic Differentiation</span> |
