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

Extend generics example with more ADT polymorphism #1

Open
henesy opened this issue Jul 19, 2019 · 0 comments
Open

Extend generics example with more ADT polymorphism #1

henesy opened this issue Jul 19, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@henesy
Copy link
Owner

henesy commented Jul 19, 2019

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);
}
@henesy henesy added the enhancement New feature or request label Jul 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant