Why does this proc definition not work and how to fix it?
proc testTableOptiion*[T]( collection: seq[T], table: Option[Table[T, float]] = none( Table[T, float] )): T =
...
This results in the error:
no generic parameters allowed for Table
or sometimes
Error: cannot instantiate: 'Table[A, B]'; Maybe generic arguments are missing?.
I need to check to see if the table exists so as to create one based on the passed-in collection if it does not.
I could check if it is empty but I want to give the user the option of not having to pass in the argument
Nim has optional named arguments. How about simply
import std/tables
proc maybeTable*[T](collection: seq[T], table=initTable[T]()): T =
if table.len == 0: discard # same if users passes an empty table
It's actually possible to do more here, like have some outer scope var or ptr and see if the call gets the default vs. a user value, but I suspect that is all more than you actually need.
could be one of those issues where "exists: empty" and "doesn't exist" are semantically distinct.
I know and these cases are to be avoided. ;-)