diff options
| author | kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> | 2024-07-15 22:31:23 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-15 20:31:23 -0700 |
| commit | bd6314de8bb113e2a317230f5e0fb6200c39a2e9 (patch) | |
| tree | 44c1827e86322a0ebc0c3e2a3f2f13e0a800dd3d /docs | |
| parent | 36e445bb8207a2ec33df4479513ebeec0bc5873e (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')
| -rw-r--r-- | docs/user-guide/03-convenience-features.md | 17 | ||||
| -rw-r--r-- | docs/user-guide/06-interfaces-generics.md | 14 | ||||
| -rw-r--r-- | docs/user-guide/toc.html | 1 |
3 files changed, 27 insertions, 5 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: diff --git a/docs/user-guide/06-interfaces-generics.md b/docs/user-guide/06-interfaces-generics.md index 7935f3551..982b2ba8b 100644 --- a/docs/user-guide/06-interfaces-generics.md +++ b/docs/user-guide/06-interfaces-generics.md @@ -677,8 +677,10 @@ T compute<T>(T a1, T a2) // compute(3, 1) == 2 ``` -`as` operator can also be used in the `if` predicate to test if an object can be casted to a specific type, once the cast test is successful, -the object can be used in the `if` block as the casted type without the need to retrieve the `Optional<T>::value` property: +Since `as` operator returns a `Optional<T>` type, it can also be used in the `if` predicate to test if an object can be +casted to a specific type, once the cast test is successful, the object can be used in the `if` block as the casted type +without the need to retrieve the `Optional<T>::value` property, for example: + ```csharp interface IFoo { @@ -704,10 +706,10 @@ void test(IFoo foo) { // This syntax will be desugared to the following: // { - // Optional<MyImpl1> $OptVar = foo as MyImpl1; - // if ($OptVar.hasValue) + // Optional<MyImpl1> optVar = foo as MyImpl1; + // if (optVar.hasValue) // { - // MyImpl1 t = $OptVar.value; + // MyImpl1 t = optVar.value; // t.foo(); // } // else if ... @@ -734,6 +736,8 @@ void main() } ``` +See [if-let syntax](convenience-features.html#if_let-syntax) for more details. + Extensions to Interfaces ----------------------------- diff --git a/docs/user-guide/toc.html b/docs/user-guide/toc.html index 2adb7655b..c1915e5dc 100644 --- a/docs/user-guide/toc.html +++ b/docs/user-guide/toc.html @@ -39,6 +39,7 @@ <li data-link="convenience-features#operator-overloading"><span>Operator Overloading</span></li> <li data-link="convenience-features#subscript-operator"><span>Subscript Operator</span></li> <li data-link="convenience-features#optionalt-type"><span>`Optional<T>` type</span></li> +<li data-link="convenience-features#if_let-syntax"><span>`if_let` syntax</span></li> <li data-link="convenience-features#reinterprett-operation"><span>`reinterpret<T>` operation</span></li> <li data-link="convenience-features#pointers-limited"><span>Pointers (limited)</span></li> <li data-link="convenience-features#struct-inheritance-limited"><span>`struct` inheritance (limited)</span></li> |
