Hi, I noticed a missing opportunity to have more symmetry in the Nim language.
Consider varargs in procedure arguments and all in pattern matching:
proc someProc(first: int, rest: varargs[int]) =
echo rest # [2, 3]
someProc(1, 2, 3)
import fusion/matching
{.experimental: "caseStmtMacros".}
case [1, 2, 3]:
of [@first, all @rest]:
echo rest # @[2, 3]
Changing the procedure argument to all would make it more symmetrical:
# proposal:
proc someProc(first: int, all rest: int) =
echo rest # [2, 3]
someProc(1, 2, 3)
Note: In JavaScript this is already possible:
function someFun(first, ...args) {
console.log(args) // [2, 3]
}
someFun(1, 2, 3)
const [first, ...rest] = [1, 2, 3]
console.log(rest) // [2, 3]
What do you think?
Best regards, David
Pattern matching is much, much, much newer than varargs.
And honestly your proposal makes the language less "symmetric", if anything, because there's no such thing in Nim to specify the type of the argument (or part of it) before the name.
Pattern matching uses that because it's more natural for patterns
Thanks Yordanico for your reply!
there's no such thing in Nim to specify the type of the argument (or part of it) before the name.
Is varargs[int] really a type in the eyes of the compiler? It looks more like a hack to capture multiple arguments.
Would be nice to have something like that
let [first, second, ..., last] = people
let [first, ...middle, last] = people
let some_people = [...middle, last]
let terran = (race: "Terran", life: 0)
let jim = (terran..., name: "Jim", life: 70)
let jim = terran & (name: "Jim", life: 70)
proc init(_: type[Unit], name: string, race: string, life: float): Unit =
Unit(name, race, life)
let jim = Unit.init(terran..., name = "Jim", life = 70)
proc something(first: int, rest...: int) =
echo rest
@alexeypetrushin, I fully agree, these would be great quality of life improvements. I also stumbled upon
Unit(name, race, life) # Instead of Unit(name: name, race: race, life: life)
@ynfle, it's nice but I'm afraid if it's solved on a library level, it will just confuse people and not find too much acceptance.