Hi,
I still get tripped up by this. Why is it that some templates allow me to write using dot notation, and others do not? Is it that the ones that do not allow dot notation have untyped parameters? - Why would that affect it? My current workflow is to memorize the list of UFCable templates by trial and error, which is annoying. I'm hoping to find a better answer for when introducing nim to others.
import sequtils, tables
var a = {0:10,1:20,2:30}.toTable()
withValue(a, 1, value) do: # OK function call use of template
value[] = 11
a.withValue(1, value) do: # OK method call use of template
value[] = 11
echo toSeq(a.pairs) # OK function call use of template
echo a.pairs.toSeq() # ERROR method call use of template
# maybe the dot notation with iterator is confusing the compiler...
echo pairs(a).toSeq() # ERROR nope, still doesn't work
Thanks!Hello.
I think that the issue is that pairs is an iterator. Unlike in C++ and Rust, iterator s are not objects, but rather a special type of resumable function.
I think I just found the reason for this: https://nim-lang.org/docs/manual.html#templates-limitations-of-the-method-call-syntax.
Apparently, the compiler rejects the use of the iterator outside of a loop context before allowing for the UFC transformation to be applied. This is a tad bit odd, but it is well-defined behavior according to the manual. So, in those cases where you are invoking a template with an iterator as an argument, you have to use the function call syntax.
Hope this helps!