The example is a bit unfortunate, I used Variant as a container and haven't 
noticed it. What I meant is the example below 
[play](https://play.nim-lang.org/#ix=4ISG).

Yes, you can use variant as a container, and manually box/unbox it, and that's 
exactly what boilerplate code is. Union types don't have such problem.
    
    
    type
      CommandKind = enum eval, replace
      Command = object
        case kind: CommandKind
        of eval:
          code:       string
        of replace:
          element_id: string
          content:    string
    
    proc exec_eval(command: Command) =
      if command.kind != eval: raise newException(Exception, "wrong")
      discard
    
    proc exec_replace(command: Command) =
      if command.kind != replace: raise newException(Exception, "wrong")
      discard
    
    proc dispatch(command: Command) =
      case command.kind
      of eval:
        exec_eval(command)
      of replace:
        exec_replace(command)
    
    let c = Command(kind: eval, code: "alert('hi')")
    dispatch(c)
    
    
    Run

Reply via email to