If you want to have better control over when the id is there, you can do 
something like this
    
    
      MsgKinds = string | int | float
      
      MsgType = enum
        mtPS,
        mtRR,
        mtR
      
      Message*[T: MsgKinds] = object
        topic*: string
        msg*: T
        case msgtype*: MsgType #"p/s", "req/rep", "reply". req/rep requires id
          of mtRR:
            id: string
          else:
            discard
    
    
    proc post*[T: MsgKinds](topic:string, msg: T, id = "", msgtype = mtPS)  =
      var message =
        case msgType
        of mtRR:
          Message[T](
            topic: topic,
            msg: msg,
            id: id,
            msgType: msgType
          )
        else:
          Message[T](
            topic: topic,
            msg: msg,
            msgType: msgType
          )
    
    
    Run

Reply via email to