From 57d86f1045fd0cb85f7182a2c7346355fa0641e6 Mon Sep 17 00:00:00 2001 From: flaviut Date: Sat, 15 Nov 2014 05:55:11 -0800 Subject: [PATCH] Updated Common Criticisms (markdown) --- Common-Criticisms.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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}`