From bda1a69da8682a00262e548a8f6c2899d15a767c Mon Sep 17 00:00:00 2001 From: Matthew Lutze Date: Fri, 7 Jun 2024 07:53:32 +0200 Subject: [PATCH] feat: add assoc examples --- src/page/Home.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/page/Home.js b/src/page/Home.js index 793c8d9..d91df6d 100644 --- a/src/page/Home.js +++ b/src/page/Home.js @@ -492,6 +492,91 @@ instance Eq[(a1, a2)] with Eq[a1], Eq[a2] { + + + + +

Associated Types

+ +

+ Flix supports associate types, which allow the types in instance signatures + to depend on the instance type. +

+ +

+ The code on the right defines a trait with an associated type Elm, + which enables each Coll instance to define its element type. +

+
+
+
+ + + + {`trait Coll[a] { + + /// The element type of the collection. + type Elm + + /// Converts the collection to a list of its elements. + def toList(coll: x): List[Coll.Elm[a]] +} + +instance Coll[Map[k, v]] { + type Elm = (k, v) + + def toList(m: Map[K, V]): List[(k, v)] = ... +} +`} + + +
+ + + + + {`trait Coll[a] { + + /// The element type of the collection. + type Elm + + /// The effect associated with the collection. + type Aef + + /// Converts the collection to a list of its elements. + def toList(coll: x): List[Coll.Elm[a]] \\ Coll.Aef[a] +} + +instance Coll[MutMap[k, v, r]] { + type Elm = (k, v) + type Aef = r + + def toList(m: Map[K, V]): List[(k, v)] \\ r = ... +} +`} + + + + + +

Associated Effects

+ +

+ Associated effects allow the effects in trait members to depend on the instance type. + This makes it easy to create abstractions over both pure and effectful operations, + and mutable and immutable data structures. +

+ +

+ The code on the left adds an associated effect Aef to the Coll trait, + which makes it possible to add instances for mutable collections. +

+
+
+
+ +
+