This page is no longer maintained — Please continue to the home page at www.scala-lang.org

When to Drop "()" in a Function Call

5 replies
hossein_haeri
Joined: 2011-09-25,
User offline. Last seen 1 year 2 weeks ago.

Dear all,

In page 108 of Odersky's "Programming in Scala, 2e", I see the
following function definition:

def h() = ...

which is also called in the following way a few lines later in the same page:

h

I was surprised to see this is a function call. The FP norm is that h

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: When to Drop "()" in a Function Call
The usual convention is:
define a method with () if it has side effectsdefine it without parenthesis if it's acting purely as a property
e.g.class circle(radius: Int) {   def diameter = radius * 2  def circumference = ...}
Of course, you could use vals here, but you couldn't if the circle was mutable
and call a method as it was defined - so don't define f and call f(), or define f() and call f


On 30 September 2011 15:59, Seyed H. HAERI (Hossein) <hossein.haeri@gmail.com> wrote:
Dear all,

In page 108 of Odersky's "Programming in Scala, 2e", I see the
following function definition:

def h() = ...

which is also called in the following way a few lines later in the same page:

h

I was surprised to see this is a function call. The FP norm is that h
hossein_haeri
Joined: 2011-09-25,
User offline. Last seen 1 year 2 weeks ago.
Re: When to Drop "()" in a Function Call

I see. Thanks.

On 30 September 2011 17:06, Kevin Wright wrote:
> The usual convention is:
> define a method with () if it has side effects
> define it without parenthesis if it's acting purely as a property
> e.g.
> class circle(radius: Int) {
>   def diameter = radius * 2
>   def circumference = ...
> }
> Of course, you could use vals here, but you couldn't if the circle was
> mutable
> and call a method as it was defined - so don't define f and call f(), or
> define f() and call f
>
>
> On 30 September 2011 15:59, Seyed H. HAERI (Hossein)
> wrote:
>>
>> Dear all,
>>
>> In page 108 of Odersky's "Programming in Scala, 2e", I see the
>> following function definition:
>>
>> def h() = ...
>>
>> which is also called in the following way a few lines later in the same
>> page:
>>
>> h
>>
>> I was surprised to see this is a function call. The FP norm is that h

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: When to Drop "()" in a Function Call

On Fri, Sep 30, 2011 at 11:59, Seyed H. HAERI (Hossein)
wrote:
> Dear all,
>
> In page 108 of Odersky's "Programming in Scala, 2e", I see the
> following function definition:
>
> def h() = ...
>
> which is also called in the following way a few lines later in the same page:
>
> h

You can drop parenthesis when calling a method with an empty parameter
list. This is possible so that Java methods, which must always have a
parameter list, could be called without parameters (eg: string.size,
array.length). In all other respects I can think of, a method without
parameter lists and a method with an empty one are different, an they
are _not_ interchangeable as structural types.

tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Re: When to Drop "()" in a Function Call
After some years with Scala I've never really gotten acquainted with def as fields. It's very confusing, especially since you can drop the empty argument list when applying methods.

On 30 September 2011 17:06, Kevin Wright <kev.lee.wright@gmail.com> wrote:
The usual convention is:
define a method with () if it has side effectsdefine it without parenthesis if it's acting purely as a property
e.g.class circle(radius: Int) {   def diameter = radius * 2  def circumference = ...}
Of course, you could use vals here, but you couldn't if the circle was mutable
and call a method as it was defined - so don't define f and call f(), or define f() and call f


On 30 September 2011 15:59, Seyed H. HAERI (Hossein) <hossein.haeri@gmail.com> wrote:
Dear all,

In page 108 of Odersky's "Programming in Scala, 2e", I see the
following function definition:

def h() = ...

which is also called in the following way a few lines later in the same page:

h

I was surprised to see this is a function call. The FP norm is that h
Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: When to Drop "()" in a Function Call
Speaking personally, I mostly use it when specifying *abstract* properties, or properties with a default constant value - which will then be overridden by a val or lazy val in some concrete subtype.
That way, I avoid the issue of the subclass inheriting an additional, and redundant, backing field from its parent.

On 1 October 2011 13:40, Trond Olsen <trond@steinbit.org> wrote:
After some years with Scala I've never really gotten acquainted with def as fields. It's very confusing, especially since you can drop the empty argument list when applying methods.

On 30 September 2011 17:06, Kevin Wright <kev.lee.wright@gmail.com> wrote:
The usual convention is:
define a method with () if it has side effectsdefine it without parenthesis if it's acting purely as a property
e.g.class circle(radius: Int) {   def diameter = radius * 2  def circumference = ...}
Of course, you could use vals here, but you couldn't if the circle was mutable
and call a method as it was defined - so don't define f and call f(), or define f() and call f


On 30 September 2011 15:59, Seyed H. HAERI (Hossein) <hossein.haeri@gmail.com> wrote:
Dear all,

In page 108 of Odersky's "Programming in Scala, 2e", I see the
following function definition:

def h() = ...

which is also called in the following way a few lines later in the same page:

h

I was surprised to see this is a function call. The FP norm is that h

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland