Changing the kind effectively erase the object. This is due to the semantics of 
variant which are supposed to represent different runtime types and changing 
the kind would reinterpret the potentially garbage-filled fields.

Note that things might have change in devel after this RFC was implemented: 
[https://github.com/nim-lang/RFCs/issues/209](https://github.com/nim-lang/RFCs/issues/209)

If that RFC didn't change anything you can:

  * compile with `-d:nimOldCaseObjects` to use the previous behaviour
  * Use your own `{.union.}` object like so:


    
    
    type ObjKind = enum
      typeA, typeB
    
    type ObjField{.union.} = object
      fieldA: int
      fieldB: float
    
    type Obj = object
      kind: ObjKind
      f: ObjField
    
    
    Run

Reply via email to