You cannot have a seq with both ElementObj[int] and ElementObj[string] in it, 
if that's what you're asking, but there are a few ways you can make it possible.

Firstly you can use [object 
variants](https://nim-lang.org/docs/manual.html#types-object-variants) to your 
advantage. The implementation would be like:
    
    
    type
      ElementKind* = enum
        ekInt, ekString
      Element = ref ElementObj
      ElementObj = object
          parent: string
          case kind: ElementKind
          of ekInt:
            intData: int
          of ekString:
            stringData: string
    
    proc newElement(parent: string, data: int): Element =
        new(result)
        result.parent = parent
        result.kind = ekInt
        result.intData = data
    
    proc newElement(parent: string, data: string): Element =
        new(result)
        result.parent = parent
        result.kind = ekString
        result.stringData = data
    
    var element = newElement("index", 12)
    
    
    Run

The second one is using object inheritance, which isn't a very common approach 
but works for OOP purposes.
    
    
    type
      Element = ref ElementObj
      ElementObj = object of RootObj
          parent: string
      
      StringElement = ref StringElementObj
      StringElementObj = object of ElementObj
        data: string
      
      IntElement = ref IntElementObj
      IntElementObj = object of ElementObj
        data: int
    
    proc newElement(parent: string, data: int): IntElement =
        new(result)
        result.parent = parent
        result.data = data
    
    proc newElement(parent: string, data: string): StringElement =
        new(result)
        result.parent = parent
        result.data = data
    
    var element = newElement("index", 12)
    
    
    Run

Note that if you want dynamic dispatch you will have to use methods instead of 
procs. This is what I mean:
    
    
    type
      Fruit = ref object of RootObj # needs to be a ref type for dynamic 
dispatch
      Apple = ref object of Fruit
      Pear = ref object of Fruit
    
    method newElement(parent: string, fruit: Fruit) =
    #proc newElement(parent: string, fruit: Fruit) =
      echo "fruit"
    
    method newElement(parent: string, apple: Apple) =
    #proc newElement(parent: string, apple: Apple) =
      echo "apple"
    
    var fruit: Fruit
    fruit = Apple()
    newElement("index", fruit) # "fruit" if you used the proc keyword, "apple" 
if you used the method keyword
    
    
    Run

And finally, this one isn't limited to a few types but is unsafe, you can also 
use the RTTI-based [typeinfo.Any](https://nim-lang.org/docs/typeinfo.html#Any). 
I haven't seen anyone actually use it though.
    
    
    import typeinfo
    
    type
      Element = ref ElementObj
      ElementObj = object
          parent: string
          data: Any
    
    proc newElement(parent: string, data: Any): Element =
        new(result)
        result.parent = parent
        result.data = data
    
    var num = 12
    var element = newElement("index", num.toAny)
    
    
    Run

Reply via email to