From d5e4854b63440009c4bcf3638a79159b2ef60dfa Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 10 Nov 2022 09:19:55 -0800 Subject: Update documentation on new features (#2508) * Update documentation on new features * Fix. Co-authored-by: Yong He --- docs/user-guide/04-interfaces-generics.md | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'docs/user-guide/04-interfaces-generics.md') 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 +{ + static const int value = T.value + 1; +} + +static const int result = GetValuePlus1.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` 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 optV = foo as MyImpl; + if (t == (optV != none)) + printf("success"); + else + printf("fail"); +} +void main() +{ + MyImpl v; + test(v); +} +// Result: +// "success" +``` + + Extensions to Interfaces ----------------------------- -- cgit v1.2.3