henesy / henesy/limbobyexample

Extend generics example with more ADT polymorphism

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Limbo
Stars
64
Forks
8
PR merge metrics
No merged PRs in 30d

Description

The `for { T => ; }` statement can be used in an adt[T] as well.

From qrstuv on #inferno:

```
implement Dongmap;

include "sys.m";
sys: Sys;
include "draw.m";

Dongmap: module {
init: fn(nil: ref Draw->Context, nil: list of string);
};

Map: adt[K, V]
for { K => hash: fn(nil: self K): int; } { # ← This right here
v: V;
add: fn(nil: self ref Map[K, V], key: K, val: V);
find: fn(nil: self ref Map[K, V], key: K): V;
};

Wang: adt {
hash: fn(nil: self ref Wang): int;
};

WingDingDingaling: adt {
sz: int;
};

Map[K, V].add(m: self ref Map[K, V], key: K, val: V)
{
m.v = val;
}

Map[K, V].find(m: self ref Map[K, V], key: K): V
{
return m.v;
}

Wang.hash(nil: self ref Wang): int
{
return 69105;
}

init(nil: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;

m := ref Map[ref Wang, ref WingDingDingaling](ref WingDingDingaling(2));
m.add(ref Wang, ref WingDingDingaling(10));
sys->print("%d\n", m.find(ref Wang).sz);
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.