Given a simple "kinded" object like: type NodeKind = enum nkParent, nkChild Node = ref NodeObj NodeObj {.acyclic.} = object case kind: NodeKind of nkParent: children: seq[Node] of nkChild: name: string let a = Node(kind: nkParent) a.name = "foo" # ideally this should not compile and the language server should not not provide "name" as an option Run
the program compiles correctly, but when it runs it throws an unhanded exception as name is not a valid field for a `nkParent`. Is there a clean recommended way to have this kind of type safety to work at compile time instead? Also the language server provides name as a possible autocomplete as well, while it would be nice to have only suggestions present on that specific kind. This seem to be a very basic expectation in terms of type safety. It is extremely easy to introduce bugs that can only be caught too late at runtime if this code compiles correctly. Maybe I am missing some core concepts here on how to idiomatically have type safety in similar use cases?