Skip to content

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

Closed
@rafrafek

Description

@rafrafek

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")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions