I noticed that calling functions without parentheses with named arguments (Smalltalk like) behaves very strange, maybe it's a bug. For example, here is the code of Crystal and similar Nim.
def some_method(x, y = 1, z = 2, w = 3)
# do something...
end
some_method 10
some_method 10, z: 10
some_method 10, w: 1, y: 2, z: 3
some_method y: 10, x: 20
proc some_proc(x: int, y = 1, z = 2, w = 3) =
discard
some_proc 10
some_proc 10, z = 10 # The first argument is not named, the others are named works
some_proc 10, w = 1, y = 2, z = 3
some_proc x = 10, y = 10 # The first argument is named - Error: invalid indentation
It "may" be a bug, but probably not. What happens is that is evaluated as someProc(x) = 10, which can be confirmed with:
import std/macros
dumptree:
someProc x = 10
Which shows what is happening it's making it an assign of the return type of the proc call, which has a functional use case with the command syntax. If you were to have a getter for a type and wanted to assign it as below:
type A = object
someField: int
proc someProc(a: var A): var int = a.someField
var test = A()
someProc test = 300
assert test.someField == 300
It's just a case of how the parser converts the statement, presently someIdent x = 100, y = 30, z = 400 is incorrect for the parser as it's an assign followed by a list of statements. see https://github.com/nim-lang/Nim/issues/15050#issuecomment-666527732
the question is: how can we improve things here without introducing parser ambiguties?
Imho parenless syntax should only work with unnamed args or even one unnamed arg. Otherwise it looks straight up horrible (this Crystal code is hardly readable imho).
Sure, theoretically it may be supported in Nim but if you actually want to call things like myProc 1, true, length = 10, then it should be considered bad style. Imho. Just put those parens around, they’re free!