Whats wrong with generic parameters/return values in lambdas? Even C++14 filally did it. Generic lambdas is 'must have' feature for functional programming. Although Nim doesn't stand as functional language, it has almost all the features of functional language. And by readimg this forum, I realized that I'm not the one who wants to use Nim in FP.
PS: And I think, that it will be better to use Nim then Haskell because of Nim's potential of use: from microcontrollers to browsers and supercomputers. Haskell fails here.
What's wrong with this?
from future import `=>`
passTwoAndTwo((x, y) => x + y)
But this example:
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 & ")"
proc map[T,U](h: Holder[T], f: T -> U): Holder[U] = newHolder[U](f(h.value))
echo(newHolder(100).map((x) => x * 2))
fails with:
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.})
Oops, operator $ should be:
proc `$`[T](h: Holder[T]): string = "Holder(" & $h.value & ")"
And if I set parameter type like this:
echo(newHolder(100).map((x: int) => $(x * 2)))
error disappears.
That's a bug and in fact a regression.
EDIT: No, it's a very hard problem to solve.
What's wrong with this?
The compiler complains that it's deprecated:
import future
proc passTwoAndTwo(f: proc(x, y: int): int) =
echo f(2, 3)
passTwoAndTwo((x, y) => x + y)
x.nim(6, 15) Warning: '(x; y): auto' has no type. Typeless parameters are deprecated; only allowed for 'template' [TypelessParam]
x.nim(6, 16) Warning: '(x; y): auto' has no type. Typeless parameters are deprecated; only allowed for 'template' [TypelessParam]
It needs to produce proc (x, y: auto): auto
Doesn't work for me:
proc passTwoAndTwo(f: proc(x, y: int): int) =
echo f(2, 3)
passTwoAndTwo(proc(x: auto; y: auto): auto = x + y)
x.nim(4, 48) Error: type mismatch: got (auto, auto)
but expected one of:
system.+(x: int)
system.+(x: int8, y: int8)
system.+(x: int16, y: int16)
system.+(x: int16)
system.+(x: float32, y: float32)
system.+(x: set[T], y: set[T])
system.+(x: float)
system.+(x: int32, y: int32)
system.+(x: T, y: T)
system.+(x: float32)
system.+(x: int64, y: int64)
system.+(x: int, y: int)
system.+(x: int64)
system.+(x: int32)
system.+(x: float, y: float)
system.+(x: int8)