proc foo[T](v: T) =
echo v
42.foo[T]
The above codes cause ambiguous in nim as [] could be used as generic instantiation and indexing. I heard there's a plan to solved it. But I can't find progress about this. Scala use () to index an array and dlang use !() to instantiate a generic template. They all ain't got this issue. Yesterday I modified nim parser to use {} to specify generic params like julialang do. Finally, it looks good.
Is it possible to change the syntax now to prevent this issue?
Another thing:
proc foo(v: int) = echo "foo", v
let a = 42
a.foo
type Bar = object
bar: proc()
let b = Bar(bar: proc() = echo "bar")
discard b.bar # It's a proc not proc call
b.bar() # This works
It's a little bit inconsistency. D solve this by distinguish function itself and function call.
void foo(int v) {
v.writeln; // UFCS like nim, equals writeln(v)
}
struct Bar { void bar() { "bar".writeln; } }
void main() {
42.foo;
Bar().bar;
static assert(is(typeof(&Bar().bar) == void delegate()));
}
Is there a plan to improve this?It's not really a big problem. Just use "fooT" or "foo(42)[T]". I don't think it warrants a syntax change.
Wait a moment, are you shure this D code is equivalent to the Nim code you just posted?
struct Bar { void bar() { "bar".writeln; } }
To me this looks like just a method (in c++ terms). And a method is not a value member of a struct. In other words it does not increase the size, it is just the object-oriented way to say this function takes Bar as first argument, and the syntax to call it is a bit different. But it is not stored at all in the struct.
type
Bar = object
proc bar(arg: Bar): void = echo "bar"