-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Require semicolon or newline before method body #13386
Comments
I agree—at least for Crystal—but it should be noted that this is already enforced in the parser, most likely to mirror Ruby. For context, it's possible in Ruby to define a parenthesis-less, parameterized method. For example, the following is valid: def f x
puts x
end In Crystal, however, this would raise a syntax error because all method headers must have parentheses around parameters. In both languages, |
Yeah, it's probably better to be strict about parameterless defs, even though it's technically unambiguous in Crystal. |
Actually, the formatter already takes care of removing the potentially ambiguous syntax. The first example from the OP formats like this: def foo : Array
(String)
[1]
end So the alternative is definitely considered ill-formed. But it's actually unambiguous and maybe it's fine to keep accepting it as valid code? |
I believe this is related! def foo : Int32
switch = true
if switch 10 else 0 end # syntax error: Error: unexpected token: "else"
end
puts foo The above doesn't compile but with an inserted def foo : Int32
switch = true
if switch ; 10 else 0 end # works
end
puts foo |
@stefandd |
The compiler currently does not require a clear delimiter between the head and body of a method.
For example,
def foo : Int32 1 end
is valid syntax.There are some oddities:
def foo 1 end
is not valid, butdef foo() 1 end
is.Some typos can cause surprising behaviour. The following code is valid:
The return type restriction is
Array
.(String)
is parsed as the first expression in the method body.A variation of that appears in #6191:
I think requiring an explicit delimiter could improve the last examples because they would lead to (meaningful) syntax errors.
This comment form @FnControlOption in #11854 (comment) makes a concrete suggestion:
I'm not sure about (a). A
def
without parenthesis cannot have parameters, so this case should be pretty unambiguous.The text was updated successfully, but these errors were encountered: