"Hello, ".echo "world!"
The above code compiles and works. What is the reasoning behind this syntax?
I understood echo "Hello, world!" and the other forms; but what is the reasoning behind allowing the above syntax?
I'm not criticising it; I just wanted to know the reasoning.
see: https://en.wikipedia.org/w/index.php?title=Uniform_Function_Call_Syntax
basically it allows method style function call without OOP
echo "hello".strip().toUpperAscii()
# ^ ^
# └-------┴--> UFCS
The dot is used to let people use object oriented syntax on objects. This site has examples of it in use https://nim-by-example.github.io/types/objects/
Often the space is used to dispatch things. Here's a package that uses this syntax to use a main object to then take functions for multithreading.
https://github.com/Araq/malebolgia
(m.spawn dfs() is used in the example)
"Hello, ".echo "world!"
Above syntax uses method call syntax and Command invocation syntax.
Method call syntax allows calling procs in object-oriented way like obj.methodName(args) and chains proc calls like x.foo.bar.baz that is simpler than baz(bar(foo(x))).
If you take all of that into account "Hello, ".echo "world!" is equivalent to echo("Hello, ", "world!")
As for the why of the UFCS and the optional parenthesis, this is my opinion:
I personally really love these features of Nim's syntax, particularly UFCS, and I wish it was adopted by other languages.
I should have been a bit more clearer with my question.
I got the method call syntax with a dot.
But I want to see the reasoning (and the use case) of allowing the argument follow a method call.
I understand: echo "Hello, world!" - This helps in faster print based debugging (also this is command invocation syntax).
echo("Hello, world!")
"Hello, world!".echo
"Hello, world!".echo()
echo "Hello, ", "world!"
I don't get "Hello, ".echo "world!" or its specific use case.
I don't get "Hello, ".echo "world!" or its specific use case.
Because myseq.add "abc" is nice to read.
Aah.
I was looking at the wrong example use case ("echo") then.
This (adding an element to a sequence) makes sense. Thanks @Araq!