diff options
| author | Yong He <yonghe@outlook.com> | 2024-04-02 12:49:06 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-02 12:49:06 -0700 |
| commit | 539d368a32ec0afa44c195615baf50dafe602563 (patch) | |
| tree | ccac2a7c77b8459e4a1e3b8f83cf230b93a9fcb8 /docs/user-guide/02-conventional-features.md | |
| parent | b5f4cf63a8b952731053a0d04af0fc8c946d86f3 (diff) | |
Update user guilde for new features. (#3875)
Diffstat (limited to 'docs/user-guide/02-conventional-features.md')
| -rw-r--r-- | docs/user-guide/02-conventional-features.md | 51 |
1 files changed, 50 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: |
