when argument of function has any generic type or some types, can those types have default type?
I have trouble that when empty sequence is set on argument of function with sequence having generic type, like following
proc f[T](a: seq[T]) =
discard
f(@[])
# this is sample, there is no meant
So I want to set any type as the default to generic type, is there such a function?
I don't entirely understand your question, but
default(T)
exists.
You can create a seq of a specific type with newSeq[T]()
sorry for the confusion, but i want to say that:
Is there any functions that will specify the type by default when a value of indeterminate type is assigned to a function argument? That's the question I wanted to ask.
Empty sequence @[] has an indeterminate type. When empty sequence is assigned to argument with generic sequence seq[T], [T] is indeterminate. So, if [T] has default type, this problem is solved.
Is there such a function?
Nim says var x = @[] is an compile error "cannot infer the type of the sequence". So I think calling proc foo[T](a: seq[T]) = with foo(@[]) also should be an compile error.
Making foo(@[]) compiles is like keep your code working even if you made a mistake. When you find something wrong in your program, it might take long time to finding the root of problem.
If you still want to pass @[] to such a generic proc, how about to explicity specify T like foo[int](@[])?