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

Expressions in type definitions are evaluated before type parameters are bound #1969

Closed
MasonMcGill opened this issue Jan 16, 2015 · 5 comments · Fixed by #24028
Closed

Expressions in type definitions are evaluated before type parameters are bound #1969

MasonMcGill opened this issue Jan 16, 2015 · 5 comments · Fixed by #24028

Comments

@MasonMcGill
Copy link

type ZeroGenerator = object
proc next(g: ZeroGenerator): int = 0

# This compiles.
type TripleOfInts = tuple
  a, b, c: type(new(ZeroGenerator)[].next)

# This raises a compiler error before it's even instantiated.
# The `new` proc can't be resolved because `Generator` is not defined.
type TripleLike[Generator] = tuple
  a, b, c: type(new(Generator)[].next)

I'm not sure if this is intended behavior or a bug. There is a workaround, but it's somewhat awkward: you can compute extra types in constructor/initializer routines, though this may lead to extraneous type parameters. e.g this:

type Peekable[Generator] = object
  first: type(new(Generator)[].next)
  rest: Generator

proc newPeekable(generator: any): auto =
  var result: Peekable[type(generator)]
  result.rest = generator
  result.first = result.rest.next
  result

becomes this:

type Peekable[Element, Generator] = object
  first: Element
  rest: Generator

proc newPeekable(generator: any): auto =
  var result: Peekable[type(generator.next), type(generator)]
  result.rest = generator
  result.first = result.rest.next
  result
@jangko
Copy link
Contributor

jangko commented Mar 24, 2017

surely this is another good example of generic early evaluation syndrom similar to #5540

@zah zah self-assigned this Mar 24, 2017
@stale
Copy link

stale bot commented Aug 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Aug 4, 2020
@ringabout ringabout removed the stale Staled PR/issues; remove the label after fixing them label Jan 8, 2021
@shirleyquirk
Copy link
Contributor

using typeof instead of type works. tested on nim 1.6.12

type ZeroGenerator = object
proc next(g:ZeroGenerator):int = 0

type TripleLike[Generator] = tuple
  a,b: typeof(new(Generator)[].next())
  c: typeof(default(Generator).next())

@metagn
Copy link
Collaborator

metagn commented Aug 27, 2023

To make it work in general we might need to update mNew/mDefault/all semMagic in the compiler to propagate tyFromExpr as in #22029, #22499

@metagn
Copy link
Collaborator

metagn commented Aug 21, 2024

Seems to work now probably because of #23983

metagn added a commit to metagn/Nim that referenced this issue Aug 29, 2024
@Araq Araq closed this as completed in fc853cb Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants