hi, I have a question about usage of enums to model a tree structure (e.g. AST).
    
    
    type
      NodeKind* = enum
        Sheet,
        Comment,
        Rule,
        AtRule,
        Decl
      
      Node* = ref NodeObj
      NodeObj* {.acyclic.} = object
        case kind*: NodeKind
        of Sheet:
          nodes*: seq[Node]
        of Comment:
          value*: string
        of Rule:
          selector*: string
          nodes*: seq[Node]
        of AtRule:
          name*: string
          params*: string
          nodes*: seq[Node]
        of Decl:
          prop*: string
          value*: string
    
    
    Run

This seem to be the pattern used on nim ast itself, my question is what would 
be the idiomatic way to restrict a type declaration to a subset of the `Node` 
enum. (e.g. nodes of `Sheet` can only be of kind `Rule`, `Comment`, and 
`AtRule`, nodes of `Rule` can only be of kind `Decl`, `Comment`, and `AtRule`, 
etc.

In Rust this is a known limitation for example and you end up needing to create 
auxiliary types (e.g. `RuleChild`, `RootChild`, etc.) leading to a quite ugly 
typing and very annoying usage and matching (partially sweetened by a good set 
of macros...). Was wandering if nim has the same limitation (as the size of the 
node must be known at compile time), or if there are better ways to model these 
pretty common scenarios.

Thanks

Reply via email to