Hi I just discovered the neat feature in nim of allowing conversions of parameters before it enters the actual proc. I'm curious however if there's a version of this that allows for a single parameter instead of an array of them?
An example for anyone to play around with:
proc toRange(a: HSlice[int, int]): HSlice[int,int] = a
proc toRange(a: int): HSlice[int,int] = a..a
proc toIntSeq(a: varargs[HSlice[int, int], toRange]): seq[int] =
for r in a:
for v in r:
result.add(v)
echo toIntSeq(20)
echo toIntSeq(0..5)
echo toIntSeq(0..5, 2) # This should error since we only want one.
https://play.nim-lang.org/#pasty=PscNQBhP
I know there are probably ways around this (like maybe having several toIntSeq overloads), but I was just curious if you could do it with a singular parameter instead?
just make it generic?
proc toIntSeq[T](a: T): seq[int] =
let a = a.toRange
Yep cool it seems to work. It also works with auto
proc toIntSeq(a: auto): seq[int] =
let a = a.toRange
Though it would be nice to see the actual type on the function signature that like how varargs[type, ...] shows....
This works:
proc toRange(a: HSlice[int, int]): HSlice[int,int] = a
proc toRange(a: int): HSlice[int,int] = a..a
proc toIntSeq(a: HSlice[int, int] | int): seq[int] =
let r = a.toRange
for v in r:
result.add(v)
echo toIntSeq(20)
echo toIntSeq(0..5)