Skip to content

Object variants with non-public kind. #41

Closed

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)

Activity

  1. treeform commented on May 25, 2026

    @treeform
    Owner

    Thank you for your issue. I have been working on flatty for a bunch of things, and I think I have fixed this in this PR: #52

    Feel free to re-open the issue if it still happens to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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