diff options
| author | Yong He <yonghe@outlook.com> | 2024-04-03 11:46:59 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-03 11:46:59 -0700 |
| commit | f8e038f58ebaade84a42ad63282fe28891c027e3 (patch) | |
| tree | 467c8ae9bfcbbdd39d2ada9cac67880ff8df3897 /docs/user-guide | |
| parent | 74d4c8ab92c1050865c421a64d9f5d3fc9a2da73 (diff) | |
Add documentation about constructors (#3879)
Diffstat (limited to 'docs/user-guide')
| -rw-r--r-- | docs/user-guide/02-conventional-features.md | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/docs/user-guide/02-conventional-features.md b/docs/user-guide/02-conventional-features.md index be931d5cd..ace4ef6a8 100644 --- a/docs/user-guide/02-conventional-features.md +++ b/docs/user-guide/02-conventional-features.md @@ -145,6 +145,25 @@ struct MyData > #### Note #### > Slang allows for a trailing semicolon (`;`) on `struct` declarations, but does not require it. +Structure types can have constructors. Constructors are defined with the `__init` keyword: + +```hlsl +struct MyData +{ + int a; + __init() { a = 5; } + __init(int t) { a = t; } +} +void test() +{ + MyData d; // invokes default constructor, d.a = 5 + MyData h = MyData(4); // invokes overloaded constructor, h.a = 4 +} +``` + +> #### Note #### +> Slang currently does not allow default values on struct members, but we intend to support them in the future. + ### Enumeration Types Enumeration types can be introduced with the `enum` keyword to provide type-safe constants for a range of values: |
