Templates are very powerful, and it's cool that Nim can do this. Nim is all 
about flexibility :-)

> in case an empty Action is needed, or to not have to type the whole 
> properties: %*{} thing (might be redundant, I was just trying out things and 
> stumbled on that. Still trying to grasp templates).

Yes. You could make your own template that does the Actions( properties:... ) 
stuff for you.
    
    
    # I used &&, but you could use whatever you wanted.
    template `&&`(fields: untyped): untyped =
      Action( properties: %*`fields` )
    
    var a2 = &&{
      "layer": 1,
      "add": true,
      "vis": false,
      "new_name": "woohoo less typing!"
    }
    
    
    Run

But, here are some examples on why the json dictionary type is not as type safe 
(dynamically typed):
    
    
    # assume all the code @mratsim wrote above is included
    
    proc doSomething(a: Action) =
      if a.Foo != 42:
        echo "sorry, not the answer to the universe"
    
    var a1 = Action(
      properties: %*{
        "foo" = 10
      }
    )
    var a2 = Action(
      properties: %*{
        # oops, I'm missing `foo`
        "bar" = "I'm a string"
      }
    )
    var a3 = Action(
      properties: %*{
        "foo" = "oops, I'm a string now!"
      }
    )
    
    doSomething(a1) # cool
    doSomething(a2) # uh-oh, I don't a have a bar, but the compiler won't tell 
me, I will get a run time Exception!
    doSomething(a3) # even worse! I won't get any error, but the result will be 
wrong! a3.foo will be interpreted as 0 and just keep going! Have fun debugging 
this one in a large program.
    
    
    Run

A Variant type on the other hand will yell at you at compile time that the 
variant is either missing the field or the field is the wrong type.

As far as the fix for Variant types go: I don't really care what syntax we end 
up going with, but I do hope it will get fixed eventually. It's more of an 
inconvenience imo. Variant types are still usable and capable without this 
feature! 

Reply via email to