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

Problem with generic lambdas #3127

Closed
vegansk opened this issue Jul 16, 2015 · 7 comments
Closed

Problem with generic lambdas #3127

vegansk opened this issue Jul 16, 2015 · 7 comments

Comments

@vegansk
Copy link
Contributor

vegansk commented Jul 16, 2015

This code won't compile:

import future

type Holder[T] = object
  value*: T

proc newHolder[T](v: T): Holder[T] = Holder[T](value: v)

proc `$`[T](h: Holder[T]): string = "Holder(" & $h.value & ")"

proc map[T,U](h: Holder[T], f: T -> U): Holder[U] = newHolder[U](f(h.value))

echo(newHolder(100).map((x) => x * 2))

with message

lambda.nim(12, 25) Warning: '(x): auto' has no type. Typeless parameters are deprecated; only allowed for 'template' [TypelessParam]
lambda.nim(12, 20) Error: type mismatch: got (Holder[system.int], proc (x: GenericParam): auto)
but expected one of:
lambda.map(h: Holder[map.T], f: proc (i0: T): U{.closure.})
system.map(data: openarray[T], op: proc (x: T): S{.closure.})
system.map(data: var openarray[T], op: proc (x: var T){.closure.})

If I supply paramerer type like this:

echo(newHolder(100).map((x: int) => x * 2))

, error disappears.

@Araq
Copy link
Member

Araq commented Jul 17, 2015

Very hard to implement. I think we might alternatively redesign 'map' to allow for better type inference instead of making the compiler even more complex.

@vegansk
Copy link
Contributor Author

vegansk commented Jul 17, 2015

This is not about system.map function and it's design. It's about type inference in compiler itself.

@Araq
Copy link
Member

Araq commented Jul 17, 2015

As if i don't know...

@petermora
Copy link
Contributor

After applying #3234 this code below works. However it is not the same as the original code posted above.

import future

type Holder[T] = object
  value*: T

proc newHolder[T](v: T): Holder[T] = Holder[T](value: v)

proc `$`[T](h: Holder[T]): string = "Holder(" & $h.value & ")"

proc map[T,U](h: Holder[T], f: T -> U): Holder[U] = newHolder[U](f(h.value))

proc myF[A,B](x: A): B = x * 2

echo(newHolder(100).map(myF))

@vegansk
Copy link
Contributor Author

vegansk commented Sep 8, 2015

@ghost
Copy link

ghost commented Aug 20, 2017

@dom96
Copy link
Contributor

dom96 commented Aug 20, 2017

Reopen if still a problem.

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

4 participants