| @@ -0,0 +1,89 @@ |
| 1 | +import std/io |
| 2 | +import std/list |
| 3 | + |
| 4 | +// User-defined derives (ADR 0019): a trait marked `@derivable(f)` is |
| 5 | +// derived as `f` applied to the value's constructor, each field replaced |
| 6 | +// by the trait's method applied to it. |
| 7 | + |
| 8 | +pub type Json { JNull JInt(Int) JStr(String) JArr(List<Json>) JObj(List<(String, Json)>) } |
| 9 | + |
| 10 | +@derivable(json_of_constructor) |
| 11 | +trait ToJson<a> { |
| 12 | + fn to_json(x: a) -> Json |
| 13 | +} |
| 14 | + |
| 15 | +// Records become objects, other constructors arrays tagged by name. |
| 16 | +fn json_of_constructor(c: Constructor<Json>) -> Json { |
| 17 | + match c.fields { |
| 18 | + [] -> JStr(c.name), |
| 19 | + [f, ..] if f.label != "" -> JObj(list.map(c.fields, fn(f) { (f.label, f.value) })), |
| 20 | + fields -> JArr([JStr(c.name), ..list.map(fields, fn(f) { f.value })]), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl ToJson<Int> { |
| 25 | + fn to_json(n) { JInt(n) } |
| 26 | +} |
| 27 | + |
| 28 | +impl ToJson<String> { |
| 29 | + fn to_json(s) { JStr(s) } |
| 30 | +} |
| 31 | + |
| 32 | +impl ToJson<List<a>> where ToJson<a> { |
| 33 | + fn to_json(xs) { JArr(list.map(xs, to_json)) } |
| 34 | +} |
| 35 | + |
| 36 | +fn render(j: Json) -> String { |
| 37 | + match j { |
| 38 | + JNull -> "null", |
| 39 | + JInt(n) -> show(n), |
| 40 | + JStr(s) -> "\"" <> s <> "\"", |
| 41 | + JArr(items) -> "[" <> join(list.map(items, render)) <> "]", |
| 42 | + JObj(entries) -> "{" <> join(list.map(entries, fn(e) { "\"" <> e.0 <> "\": " <> render(e.1) })) <> "}", |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn join(xs: List<String>) -> String { |
| 47 | + match xs { [] -> "", [x] -> x, [x, ..rest] -> x <> ", " <> join(rest) } |
| 48 | +} |
| 49 | + |
| 50 | +// A second derivable trait, with an Int result: the number of nodes. |
| 51 | +@derivable(count_nodes) |
| 52 | +trait Size<a> { |
| 53 | + fn size(x: a) -> Int |
| 54 | +} |
| 55 | + |
| 56 | +fn count_nodes(c: Constructor<Int>) -> Int { |
| 57 | + list.fold(c.fields, 1, fn(n, f) { n + f.value }) |
| 58 | +} |
| 59 | + |
| 60 | +impl Size<Int> { |
| 61 | + fn size(_) { 1 } |
| 62 | +} |
| 63 | + |
| 64 | +@derive(ToJson, Show) |
| 65 | +pub type User { User(name: String, age: Int, tags: List<String>) } |
| 66 | + |
| 67 | +@derive(ToJson, Size) |
| 68 | +pub type Shape { Dot Circle(Int) Rect(Int, Int) } |
| 69 | + |
| 70 | +// Generic: requires the trait of its parameter; recursive through it. |
| 71 | +@derive(ToJson, Size, Debug) |
| 72 | +pub type Tree<a> { Leaf Node(Tree<a>, a, Tree<a>) } |
| 73 | + |
| 74 | +pub fn main() -> () / <io> { |
| 75 | + let ann = User("ann", 41, ["admin", "ops"]) |
| 76 | + io.println(render(to_json(ann))) |
| 77 | + io.println(show(ann)) |
| 78 | + io.println(render(to_json([Dot, Circle(2), Rect(3, 4)]))) |
| 79 | + let tree = Node(Node(Leaf, 1, Leaf), 2, Leaf) |
| 80 | + io.println(render(to_json(tree))) |
| 81 | + io.println(debug(tree)) |
| 82 | + io.println(show(size(tree)) <> " " <> show(size(Rect(3, 4))) <> " " <> show(size(Dot))) |
| 83 | +} |
| 84 | +//> {"name": "ann", "age": 41, "tags": ["admin", "ops"]} |
| 85 | +//> User(ann, 41, [admin, ops]) |
| 86 | +//> ["Dot", ["Circle", 2], ["Rect", 3, 4]] |
| 87 | +//> ["Node", ["Node", "Leaf", 1, "Leaf"], 2, "Leaf"] |
| 88 | +//> Node(Node(Leaf, 1, Leaf), 2, Leaf) |
| 89 | +//> 7 3 1 |