Skip to content

arity-0 function "hello" performs side effects and should be declared with empty parentheses #2357

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

Closed
rafrafek opened this issue Mar 29, 2022 · 0 comments · Fixed by #2358
Closed

Comments

@rafrafek
Copy link

Function from https://docs.scala-lang.org/scala3/book/taste-hello-world.html

@main def hello = println("Hello, world!")

is arity-0 and performs side effects, therefore it should look like this:

@main def hello() = println("Hello, world!")

https://docs.scala-lang.org/scala3/book/methods-most.html says:

A suggestion about methods that take no parameters

When a method takes no parameters, it’s said to have an arity level of arity-0. Similarly, when a method takes one parameter it’s an arity-1 method. When you create arity-0 methods:

If the method performs side effects, such as calling println, declare the method with empty parentheses
If the method does not perform side effects—such as getting the size of a collection, which is similar to accessing a field on the collection—leave the parentheses off

For example, this method performs a side effect, so it’s declared with empty parentheses:

def speak() = println("hi")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant