shithub: limbobyexample

Download patch

ref: b88feaef76f1c93fc3e0c4125a8bbb2f536878e3
parent: eac284d17b14eb4602387eb47c644fcf4f6e86c0
author: seh <henesy.dev@gmail.com>
date: Mon Mar 11 00:40:35 EDT 2019

add isolated pick tag adt example

--- a/Generics/generics.b
+++ b/Generics/generics.b
@@ -14,12 +14,24 @@
 	init: fn(nil: ref Draw->Context, nil: list of string);
 };
 
+# Polymorphic type
 Rd: adt[T] {
 	t:	ref Iobuf;
 	ws:	fn(rd: self ref Rd[T]): int;
 };
 
-# Polymorphic type
+Num: adt {
+	pick {
+		String =>
+			s: string;
+		Int =>
+			d: int;
+	}
+
+	stringify: fn(v: self ref Num): string;
+};
+
+# Polymorphic and pick-tagged type
 Word: adt[T] {
 	w: T;
 
@@ -50,12 +62,18 @@
 	c := rd.ws();
 	print("Broke on '%c'\n", c);
 
+	d := ref Num.Int(5);
+	s := ref Num.String("Five");
+
+	print("%s\n", d.stringify());
+	print("%s\n", s.stringify());
+
 	words: list of ref Word[Int].String;
 
 	smiley := "☺";
 	frowny := ":(";
 
-	sword := ref Word[Int].String(Int(5), smiley);
+	sword := ref Word[Int].String(Int(9), smiley);
 
 	# Format is: Adt[Type].PickTag(fields...)
 	words = sword :: words;
@@ -78,6 +96,17 @@
 Word[T].eq(a, b: ref Word): int
 {
 	return a.w == b.w;	# This is a shallow comparison, don't rely on this
+}
+
+Num.stringify(v: self ref Num): string {
+	pick xv := v {
+	String =>
+		return xv.s;
+	Int =>
+		return string xv.d;
+	* =>
+		raise "type error";
+	}
 }
 
 # Matches whitespace characters