I am using dup for the first time:
pnt1.mirror(pnt2)
var pnt4 = pnt1.dup(mirror(pnt2)) # <--- A bit ugly
Any idea about how to make the second line better looking?
For me it would be nice if the following was possible:
pnt1.mirror(pnt2)
var pnt4 = pnt1.mirror(pnt2)
Somehow very, very few languages allow your preferred syntax. Ada's one:
with Text_IO; use Text_IO;
procedure Example is
function F return String is ("Hello, world!");
function F return Integer is (111);
begin
Put_Line (F);
Put_Line (Integer'Image (F));
end Example;
where the only way to know which F is called where is to know what type is expected back from it. This might be a style limitation rather than a technical one.
Here's an option that works now with Nim:
var pnt4 = pnt1.dup: mirror(pnt2)
pnt1.mirror(pnt2)
var pnt4 = pnt1
pnt4.mirror(pnt2)
This is exactly what dup does.
@mantielero This var pnt4 = pnt1.mirror(pnt2) would make mirror "return type generic" in a pretty radical way: depending on call context, it sometimes returns something (the dup version) and sometimes nothing at all (the typical case of the original in-place version). This is probably not orthogonal with the distinction between proc and func: the original mirror might have no other side effect than the in-place change to its var parameter and could still be a proc with some justification, but it's dup version is a clear candidate for func.
... where the only way to know which F is called where is to know what type is expected back from it. This might be a style limitation rather than a technical one.
@jrfondren Exactly because return type overloading is so rare, I doubt that it's just a style consideration. It's got to make type inference/type reasoning harder.
Any idea about how to make the second line better looking? > For me it would be nice if the following was possible: [...]
https://github.com/nim-lang/Nim/pull/13337 proposes a syntax superior to dup since it avoids nesting with parens:
doAssert @[2,1,3].>sort() == @[1,2,3]
doAssert 3.>bar(4, _) == 3 + 4 # use placeholder `_` to specify a position > 0
# can chain:
@[1].>fun(2).>fun(3).process.process.>fun(4).>fun(5)
and it unrolls into efficient code (I had some additional commits in that PR but PR got closed in meantime)
proc merge[T](result: var seq[T], x, y: openArray[T]) = ...
let a = merge(_, [1,3], [2,5])
# instead of:
let a = seq[int].default.dup(merge([1,3], [2,5]))
A Christmas wish at Araq andtimothee :
Please bury the hatchet! Together you can advance Nim much more successful.
Happy Christmas, Helmut