You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Support the outplace argument placeholder _ to convert an inplace algorithm into an outplace one for the common case where the outplace argument is assumed to be initialized to its default value for its (inferred) type.
Example
procmerge[T](result: varseq[T], x, y: openArray[T]) =...let x = [1,3]
let y = [2,5]
let a =merge(_, x, y)
let a2 = _.merge(x,y) # also works# same as:var a: seq[int]
merge(a, x, y)
# also same as:import std/sugar
let a3 =seq[int].default.dup(merge(x, y))
# less readable and not DRY since seq[int] must be specified (can't do type inference here)
Notes
the outplace argument can appear in any position, not just the 1st, as its position is given explicitly by _:
procfn[T](a: seq[T], b: var T) =...let a =fn(@[1,2], _) # workslet a2 =@[1,2].fn(_) # also works
sugar.dup is still useful in the cases where you need a non-default value for the outplace argument, or when type inference wouldn't be possible, because the generic is only inferred from the outplace argument instead of other arguments.
implementation
requires compiler support.
in sigmatch, the arguments are implicitly reordered so that _ is matched last, at which point all generic parameters must be bound, or a sigmatch error occurs.
I don't mean to delay or complicate anything, but thoughts on this?
procmerge[T](result: varseq[T], x, y: openArray[T]) =...let x = [1, 3]
let y = [2, 5]
merge(let a, x, y)
(let a2).merge(x,y) # torn on this oneprocfn[T](a: seq[T], b: var T) =...fn(@[1, 2], let a)
@[1, 2].fn(let a2)
I believe some other language does something like this but I forget
proposal
Support the outplace argument placeholder
_
to convert an inplace algorithm into an outplace one for the common case where the outplace argument is assumed to be initialized to its default value for its (inferred) type.Example
Notes
_
:sugar.dup
is still useful in the cases where you need a non-default value for the outplace argument, or when type inference wouldn't be possible, because the generic is only inferred from the outplace argument instead of other arguments.implementation
_
is matched last, at which point all generic parameters must be bound, or a sigmatch error occurs.links
The text was updated successfully, but these errors were encountered: