flatty icon indicating copy to clipboard operation
flatty copied to clipboard

Object variants with non-public kind.

Open kerrycobb opened this issue 1 year ago • 0 comments

I would expect this to work but I get an error: Error: undeclared field: 'kind' for type...

# moduleA.nim

type 
  Kinds* = enum A, B, C
  Obj* = object
    case kind: Kinds
    of A: a: string
    else: b: string 

proc newObj*(kind: Kinds): Obj = Obj(kind:kind)
proc kind*(o: Obj): Kinds = o.kind
import flatty
import ./moduleA

let
  a = newObj(B)
  serialized = toFlatty(a)
writeFile("tmp.flat", serialized)
let
  content = readFile("tmp.flat")
  x = fromFlatty(content, Obj)

Making the kind attribute public eliminates the error but I think that should not be necessary. It's also not ideal because then it's possible to change kind which I want to disallow:

# moduleA.nim

type 
  Kinds* = enum A, B, C
  Obj* = object
    case kind*: Kinds
    of A: a: string
    else: b: string 

proc newObj*(kind: Kinds): Obj = Obj(kind:kind)
import flatty
import ./moduleA

let
  a = newObj(B)
  serialized = toFlatty(a)
a.kind = C # Want to disallow this
writeFile("tmp.flat", serialized)
let
  content = readFile("tmp.flat")
  x = fromFlatty(content, Obj)

kerrycobb avatar Oct 14 '24 18:10 kerrycobb