summaryrefslogtreecommitdiff
path: root/docs/user-guide/06-interfaces-generics.md
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-09-05 15:12:52 -0700
committerGitHub <noreply@github.com>2024-09-05 15:12:52 -0700
commit5f1ba7b614b64d24fb45a3815a9867c68dace466 (patch)
tree40407853a5147e415ca3ad35ed03e1dadfb75cb1 /docs/user-guide/06-interfaces-generics.md
parent313677160a186efebf83fab4df7d08dd119a5cd2 (diff)
Various documentation improvements. (#5017)
Diffstat (limited to 'docs/user-guide/06-interfaces-generics.md')
-rw-r--r--docs/user-guide/06-interfaces-generics.md71
1 files changed, 71 insertions, 0 deletions
diff --git a/docs/user-guide/06-interfaces-generics.md b/docs/user-guide/06-interfaces-generics.md
index 61bc43f89..df51ac950 100644
--- a/docs/user-guide/06-interfaces-generics.md
+++ b/docs/user-guide/06-interfaces-generics.md
@@ -104,6 +104,22 @@ Generic value parameters can also be defined using the traditional C-style synta
void g1<typename T, int n>() { ... }
```
+Slang allows multiple `where` clauses, and multiple interface types in a single `where` clause:
+```csharp
+struct MyType<T, U>
+ where T: IFoo, IBar
+ where U : IBaz<T>
+{
+}
+// equivalent to:
+struct MyType<T, U>
+ where T: IFoo
+ where T : IBar
+ where U : IBaz<T>
+{
+}
+```
+
Supported Constructs in Interface Definitions
-----------------------------------------------------
@@ -793,6 +809,61 @@ void main()
See [if-let syntax](convenience-features.html#if_let-syntax) for more details.
+Generic Interfaces
+------------------
+
+Slang allows interfaces themselves to be generic. A common use of generic interfaces is to define the `IEnumerable` type:
+```csharp
+interface IEnumerator<T>
+{
+ This moveNext();
+ bool isEnd();
+ T getValue();
+}
+
+interface IEnumerable<T>
+{
+ assoicatedtype Enumerator : IEnumerator<T>;
+ Enumerator getEnumerator();
+}
+```
+
+You can constrain a generic type parameter to conform to a generic interface:
+```csharp
+void traverse<TElement, TCollection>(TCollection c)
+ where TCollection : IEnumerable<TElement>
+{
+ ...
+}
+```
+
+
+Generic Extensions
+----------------------
+You can use generic extensions to extend a generic type. For example,
+```csharp
+interface IFoo { void foo(); }
+interface IBar { void bar(); }
+
+struct MyType<T : IFoo>
+{
+ void foo() { ... }
+}
+
+// Extend `MyType<T>` so it conforms to `IBar`.
+extension<T:IFoo> MyType<T> : IBar
+{
+ void bar() { ... }
+}
+// Equivalent to:
+__generic<T:IFoo>
+extension MyType<T> : IBar
+{
+ void bar() { ... }
+}
+```
+
+
Extensions to Interfaces
-----------------------------