From 018e91979aa921edce7e27f259aa91893add7939 Mon Sep 17 00:00:00 2001 From: Callum Renwick Date: Thu, 31 Dec 2020 14:25:10 +0000 Subject: [PATCH] type expressions --- type_expressions/main.pony | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 type_expressions/main.pony diff --git a/type_expressions/main.pony b/type_expressions/main.pony new file mode 100644 index 0000000..cb6b696 --- /dev/null +++ b/type_expressions/main.pony @@ -0,0 +1,30 @@ +actor Main + new create(env: Env) => + env.out.print("hlo wld") + +// A tuple type is a sequence of types +var x: (String, U64) +x = ("hi", 3) +x = ("bye", 7) + +// Pony supports tuple unpacking but calls it "destructuring" +(var y, var z) = x + +// You can access the elements of tuples directly using this +// one weird syntax +y = x._1 +z = x._2 + +// You can't assign elements of tuples. To change the value +// of a tuple you must reassign the whole thing. +x = ("wibble wobble", x._2) + +// A union is a set of possible types +// This means "a is a String or None": +var a: (String | None) +a = "yeah yeah whatever static analysis tool" + +// An intersection is a value that is more than one type at once +// An example from the standard library: +// type Map[K: (Hashable box & Comparable[K] box), V] is HashMap[K, V, HashEq[K]] +// K is Hashable *and* Comparable[K] at the same time