summaryrefslogtreecommitdiff
path: root/docs/user-guide/04-interfaces-generics.md
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-11-10 09:19:55 -0800
committerGitHub <noreply@github.com>2022-11-10 09:19:55 -0800
commitd5e4854b63440009c4bcf3638a79159b2ef60dfa (patch)
treecb8d077ff01618a254d50357ad1f7dbde6617202 /docs/user-guide/04-interfaces-generics.md
parent004f6e30b5df3a3df2c26fe5c4a5e78c49f71166 (diff)
Update documentation on new features (#2508)
* Update documentation on new features * Fix. Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'docs/user-guide/04-interfaces-generics.md')
-rw-r--r--docs/user-guide/04-interfaces-generics.md54
1 files changed, 54 insertions, 0 deletions
diff --git a/docs/user-guide/04-interfaces-generics.md b/docs/user-guide/04-interfaces-generics.md
index 65c23d98e..dccaa045f 100644
--- a/docs/user-guide/04-interfaces-generics.md
+++ b/docs/user-guide/04-interfaces-generics.md
@@ -155,6 +155,26 @@ struct MyObject : IComparable
```
In this example, the `IComparable` interface declares that any conforming type must provide a `comparesTo` method that performs a comparison between an object to another object of the same type. The `MyObject` type satisfies this requirement by providing a `comparesTo` method that accepts a `MyObject` typed argument, since in the scope of `MyObject`, `This` type is equivalent to `MyObject`.
+### Static Constants
+
+You can define static constant requirements in an interface. The constants can be accessed in places where a compile-time constant is needed.
+```csharp
+interface IMyValue
+{
+ static const int value;
+}
+struct MyObject2 : IMyValue
+{
+ static const int value = 2;
+}
+struct GetValuePlus1<T:IMyValue>
+{
+ static const int value = T.value + 1;
+}
+
+static const int result = GetValuePlus1<MyObject2>.value; // result == 3
+```
+
### Initializers
Consider a generic method that wants to create and initialize a new instance of generic type `T`:
@@ -616,6 +636,40 @@ extension MyObject : IBar, IBar2
}
```
+`is` and `as` Operator
+----------------------------
+
+You can use `is` operator to test if an interface-typed value is of a specific concrete type, and use `as` operator to downcast the value into a specific type.
+The `as` operator returns an `Optional<T>` that is not `none` if the downcast succeeds.
+
+```csharp
+interface IFoo
+{
+ int foo();
+}
+struct MyImpl : IFoo
+{
+ int foo() { return 0; }
+}
+void test(IFoo foo)
+{
+ bool t = foo is MyImpl; // true
+ Optional<MyImpl> optV = foo as MyImpl;
+ if (t == (optV != none))
+ printf("success");
+ else
+ printf("fail");
+}
+void main()
+{
+ MyImpl v;
+ test(v);
+}
+// Result:
+// "success"
+```
+
+
Extensions to Interfaces
-----------------------------