summaryrefslogtreecommitdiff
path: root/docs/user-guide/03-convenience-features.md
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2024-07-15 22:31:23 -0500
committerGitHub <noreply@github.com>2024-07-15 20:31:23 -0700
commitbd6314de8bb113e2a317230f5e0fb6200c39a2e9 (patch)
tree44c1827e86322a0ebc0c3e2a3f2f13e0a800dd3d /docs/user-guide/03-convenience-features.md
parent36e445bb8207a2ec33df4479513ebeec0bc5873e (diff)
Move if_let syntax to convenience-features section (#4628)
* Move if_let syntax to convenience-features section * Fix the syntax for setting up an anchor * update the comment on sample code * Add example for if_let syntax * Address the comments
Diffstat (limited to 'docs/user-guide/03-convenience-features.md')
-rw-r--r--docs/user-guide/03-convenience-features.md17
1 files changed, 17 insertions, 0 deletions
diff --git a/docs/user-guide/03-convenience-features.md b/docs/user-guide/03-convenience-features.md
index f4cd51b3f..dc1723ebe 100644
--- a/docs/user-guide/03-convenience-features.md
+++ b/docs/user-guide/03-convenience-features.md
@@ -383,6 +383,23 @@ int caller()
}
```
+## `if_let` syntax
+Slang supports `if (let name = expr)` syntax to simplify the code when working with `Optional<T>` value. The syntax is similar to Rust's
+`if let` syntax, the value expression must be an `Optional<T>` type, for example:
+
+```csharp
+Optional<int> getOptInt() { ... }
+
+void test()
+{
+ if (let x = getOptInt())
+ {
+ // if we are here, `getOptInt` returns a value `int`.
+ // and `x` represents the `int` value.
+ }
+}
+```
+
## `reinterpret<T>` operation
Sometimes it is useful to reinterpret the bits of one type as another type, for example: