> help us to define what Nim's type system actually is

Let's see.
    
    
    type
      Atype[T] = object
        data    :   T
        inum    :   int
      Btype[T] = object
        data    :   T
        iinum   :   int
        # Atype and Btype are isomorphic, however they do not have the same 
field names
      Stype[U] = Atype[U] or Btype[U]
    
    
    # illtyped, 'a' does not necessarily have an 'inum'
    proc stprint[T : Stype[string]] ( a : T) =
      echo a.data," ",a.inum
    
    # correctly typed, but...
    proc stprint[T : Btype[int]] ( a : T) =
      echo a.data," ",a.iinum
    
    # type envelope for stprint not complete, Stype[int] is missing
    proc tprint[T : Stype](a : T) =
      stprint a
    
    proc run() =
      var vas = Atype[string]( data: "thatsit", inum : 7)
      var vbi = Btype[int]( data: 42, iinum : 5 )
      var vbs = Btype[string]( data: "youbetterdontcalltprintwithme", iinum : 
12)
      tprint(vas)
      tprint(vbi)
      #tprint(vbs)
      echo vbs.data," ",vbs.iinum
    
    run()
    
    
    
    
    
    Run

This compiles silently. But is not correctly typed. If the compiler had to 
build a virtual function table, it would complain (vft can't be constructed). 

Reply via email to