4 ms·
Last time I looked at Nim, I was put off by the lack of algebraic data types and pattern matching. I still can't find anything about them in the manual. Am I mi
by marijn 12y ago
Last time I looked at Nim, I was put off by the lack of algebraic data types and pattern matching. I still can't find anything about them in the manual. Am I missing something, or is this something it just doesn't have?
- def- 12y agoInstead of ADTs there are object variants in Nim: http://nim-lang.org/manual.html#object-variants http://nim-lang.org/manual.html#object-variants With ADTs: data Shape a = Rectangle { x :: Float , y :: Float , width :: Float , height :: Float } | Circle { x :: Float , y :: Float , radius :: Float } With Object Variants, note that we don't have to repeat x and y: type ShapeKind = enum Rectangle, Circle Shape = object x, y: float case kind: ShapeKind of Rectangle: width, height: float of Circle: radius: float The pattern matching in Nim is quite limited. Something like this works: let (x, y) = getCoordinates() But Nim doesn't have (x,y) = (y,x) for example, instead swap can be used. You could implement your own pattern matching using metaprogramming: http://www.drdobbs.com/open-source/nimrod-a-new-systems-programming-languag/240165321 http://www.drdobbs.com/open-source/nimrod-a-new-systems-prog...