- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
When to Drop "()" in a Function Call
Fri, 2011-09-30, 15:59
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
Fri, 2011-09-30, 16:27
#2
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
Fri, 2011-09-30, 17:27
#3
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.
Sat, 2011-10-01, 13:57
#4
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:
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
Sat, 2011-10-01, 14:07
#5
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:
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
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: