nim-lang / RFCs

A repository for your Nim proposals.
135 stars 26 forks source link

outplace argument `_` #292

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

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

proc merge[T](result: var seq[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

implementation

links

metagn commented 3 years ago

I don't mean to delay or complicate anything, but thoughts on this?

proc merge[T](result: var seq[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 one

proc fn[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

github-actions[bot] commented 7 months ago

This RFC is stale because it has been open for 1095 days with no activity. Contribute a fix or comment on the issue, or it will be closed in 30 days.