Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concepts: invalid type in this context for var #13046

Open
mratsim opened this issue Jan 5, 2020 · 2 comments
Open

Concepts: invalid type in this context for var #13046

mratsim opened this issue Jan 5, 2020 · 2 comments

Comments

@mratsim
Copy link
Collaborator

mratsim commented Jan 5, 2020

The following doesn't compile and gives invalid type in this context for var

Error: invalid type: 'Queue[module_name.FooWrapper]' in this context: 'FooWrapper' for var
type
  Foo = ptr object
    next: Foo

  Enqueueable = concept x, type T
    x is ptr
    x.next is T

  FooWrapper = ptr object
    next: FooWrapper
    foo: Foo
    queue: Queue[FooWrapper]

  Queue[T: Enqueueable] = object
    count: int
    back: T

# Ok so far
var q: Queue[Foo]


# ------------------------------

static: echo FooWrapper is Enqueueable
var fw: FooWrapper
@Araq
Copy link
Member

Araq commented Jan 6, 2020

Workaround:Use T instead of Enqueueable and rely on type instantiations to catch mistakes like "foobar has no field named 'next'".

@mratsim
Copy link
Collaborator Author

mratsim commented Jan 6, 2020

"All problems in computer science can be solved by another level of indirection" - David Wheeler

The following compiles (declaration order may have a play there, I didn't fully figure out the differences).

type
  Enqueueable* {.explain.} = concept x, type T
    x is ptr
    x.next is T

  Queue*[T: Enqueueable] = object
    count: int
    back: T

  Foo = ptr object
    next: Foo

  FooWrapper = ptr object
    next: FooWrapper
    foo: Foo
    bar: Bar

  Bar = object
    queue: Queue[FooWrapper]

# Ok so far
var q: Queue[Foo]


# ------------------------------

static: echo FooWrapper is Enqueueable
var fw: FooWrapper

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

No branches or pull requests

2 participants