diff --git a/Common-Criticisms.md b/Common-Criticisms.md index 1cb5218..682aa96 100644 --- a/Common-Criticisms.md +++ b/Common-Criticisms.md @@ -12,4 +12,23 @@ var b: T = ... foo(b) # also valid ``` -Note that the difference between what happens in Java and what Nim does is simply a matter of efficiency: Nim does not require our `T` to be allocated on the heap, and it certainly allows `b` to be declared with `let`, which will force an compile-time error to be thrown. \ No newline at end of file +Note that the difference between what happens in Java and what Nim does is simply a matter of efficiency: Nim does not require our `T` to be allocated on the heap, and it certainly allows `b` to be declared with `let`, which will force an compile-time error to be thrown. + +## Sum types are weird +In OCaml, sum types are denoted like this: +``` ocaml + type OptionalInt = + | Value of int + | Missing +``` +while in Nim they are written like +``` nimrod +type + NodeKind = enum opValue, opAdd, opSub, opMul, opCall + Node = ref object + case k: NodeKind + of opValue: value: int + of opAdd, opSub, opMul, opCall: kids: seq[Node] +``` + +While the OCaml way looks better, the Nim version allows for multiple types to have the same values, without repetition. It also has the advantage that multiple variants can concisely be matched upon using sets like `{opSub, opMul}`