Hello people,
I am hitting a weird problem with using typedesc as arguments to procs. Consider this example:
proc test(T : typedesc[SomeNumber] = float, a : T = 0.5) : T =
return a * 2
echo test(a = 0.5)
I'd expect T to simply default to float, but it won't compile unless I expliclitly call it with
echo test(float, 0.5)
Is this intendend behaviour? How can I go around it to not having to express the typedesc when it's already defaulted to a value?
But there is indeed some form of an issue maybe...
proc test(T: typedesc[SomeNumber] = float) =
discard
test(int)
proc test2(T: typedesc[SomeNumber] = float; i: int = 1) =
discard
test2(i = 1)
test2(1)
The last proc call does not work:
/tmp/hhh/t.nim(11, 6) Error: type mismatch: got <int literal(1)>
but expected one of:
proc test2(T: typedesc[SomeNumber] = float; i: int = 1)
first type mismatch at position: 1
required type for T: typedesc[SomeNumber]
but expression '1' is of type: int literal(1)
expression: test2(1)
But it is a general property of Nim default parameters:
proc t3(x: float = 0.0; s: string) =
discard
proc t4(s: string; x: float = 0.0) =
discard
#t3("hallo")
t4("hallo")
The commented out t3() call does not compile. So we should use the proc vars with defaults after other vars. Maybe we should read tutorials or the manual again.
I know I can use generics for an use case that simple, I just wanted to make a clear simple example.
I am asking about typedescs specifically for a code-generation macro that I am dealing with.
Anyway, I solved the problem by injecting the default value of the typedesc together with the value of the arguments.