From f8e038f58ebaade84a42ad63282fe28891c027e3 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 3 Apr 2024 11:46:59 -0700 Subject: Add documentation about constructors (#3879) --- docs/user-guide/02-conventional-features.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'docs') 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: -- cgit v1.2.3