Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add assoc examples #144

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/page/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,91 @@ instance Eq[(a1, a2)] with Eq[a1], Eq[a2] {
</Col>
</Row>

<Row className="mb-4">
<Col md="6">
<Card className="border-0">
<CardBody>
<CardTitle><h4>Associated Types</h4></CardTitle>
<CardText>
<p>
Flix supports associate types, which allow the types in instance signatures
to depend on the instance type.
</p>

<p>
The code on the right defines a trait with an associated type <code>Elm</code>,
which enables each <code>Coll</code> instance to define its element type.
</p>
</CardText>
</CardBody>
</Card>
</Col>
<Col md="6">
<InlineEditor>
{`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)] = ...
}
`}
</InlineEditor>
</Col>
</Row>

<Row className="mb-4">
<Col md="6">
<InlineEditor>
{`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 = ...
}
`}
</InlineEditor>
</Col>
<Col md="6">
<Card className="border-0">
<CardBody>
<CardTitle><h4>Associated Effects</h4></CardTitle>
<CardText>
<p>
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.
</p>

<p>
The code on the left adds an associated effect <code>Aef</code> to the <code>Coll</code> trait,
which makes it possible to add instances for mutable collections.
</p>
</CardText>
</CardBody>
</Card>
</Col>
</Row>

<Row className="mb-4">
<Col md="6">
<Card className="border-0">
Expand Down
Loading