Skip to content

Object variants with non-public kind. #41

@kerrycobb

Description

@kerrycobb

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions