patty icon indicating copy to clipboard operation
patty copied to clipboard

Cannot Instantiate Error is Caused in Some Cases

Open loloicci opened this issue 5 years ago • 1 comments

when compile

variant ActionTableItem[T]:
  Shift(state: State)
  Reset(rule: T)
  Accept
  Error

then,

Error: cannot instantiate ActionTableItem
got: <T>
but expected: <T>

It looks that this is caused when there are two or more kinds with no field.

loloicci avatar Mar 18 '19 08:03 loloicci

I am not sure what the error is. It seems that the generated tree is fine. In fact, if I write eplicitly what patty generates (as -d:pattydebug tells me) I get something that compiles:

type State = object

type
  ActionTableItemKind {.pure.} = enum
    Shift, Reset, Accept, Error
  ActionTableItem[T] = object
    case kind: ActionTableItemKind
    of ActionTableItemKind.Shift:
        state: State

    of ActionTableItemKind.Reset:
        rule: T

    of ActionTableItemKind.Accept:
        nil

    of ActionTableItemKind.Error:
        nil

proc `==`[T](a: ActionTableItem[T]; b: ActionTableItem[T]): bool =
  if a.kind == b.kind:
    case a.kind
    of ActionTableItemKind.Shift:
      return a.state == b.state
    of ActionTableItemKind.Reset:
      return a.rule == b.rule
    of ActionTableItemKind.Accept:
      return true
    of ActionTableItemKind.Error:
      return true
    else:
      return false

proc Shift[T](state: State): ActionTableItem[T] =
  ActionTableItem[T](kind: ActionTableItemKind.Shift, state: state)

proc Reset[T](rule: T): ActionTableItem[T] =
  ActionTableItem[T](kind: ActionTableItemKind.Reset, rule: rule)

proc Accept[T](): ActionTableItem[T] =
  ActionTableItem[T](kind: ActionTableItemKind.Accept)

proc Error[T](): ActionTableItem[T] =
  ActionTableItem[T](kind: ActionTableItemKind.Error)

andreaferretti avatar Mar 18 '19 09:03 andreaferretti