Hi, is there syntax available for specifying default arguments to a generic proc based on type? For example (syntax doesnt work):
proc get_a_value[ T: int | float](defaultValue: T int = low(int) | float = NegInf): T =
var raw = getSomeStringFromInput()
if raw == "":
return defaultValue
when T is int:
return parseInt(raw)
else:
return parseFloat(raw)
This works:
func foo[T: int|float](x: T = when T is int: 3 else: 4.0): T =
x * x
echo foo[int]()
echo foo[float]()
echo foo(6)
Though with more arguments, it would probably be better to extract it into a separate function for readability.
Thanks, I did not think of the ternary style of providing an argument, that is neat.
I agree regarding the separate function, I ended up writing a fooAux procedure which was called by overloaded versions of foo with default arguments.