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

Why is there a difference between def and val for methods and functions?

3 replies
Philip May
Joined: 2010-04-04,
User offline. Last seen 42 years 45 weeks ago.
Hello,

why is there a difference between def and val for methods and functions?

In this following example both methods / functions seem to do and seem to feel like the same...

class MyScalaClass {
  def addOneWithDef(x: Int) = x + 1
  val addOneWithVal = (x: Int) => x + 1
}

The only difference is that you seem to tell addOneWithDef "a method" and
addOneWithVal "a function value".

But for me it seems to be or at least it feels like the same thing...
Is it about Java compatibility?

Greetings,
Philip May

--
Philip May <eniak.info@googlemail.com>
Facebook: http://www.facebook.com/philip.may
Blog: http://blog.eniak.info - Twitter: http://twitter.com/pMay
Lifestream: http://friendfeed.com/pmay - Visitenkarte: http://eee.am/Eniak
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: Why is there a difference between def and val for methods a

>>>>> "Philip" == Philip May writes:

Philip> Hello, why is there a difference between def and val for
Philip> methods and functions?

Philip> In this following example both methods / functions seem to do
Philip> and seem to feel like the same...

Philip> class MyScalaClass { def addOneWithDef(x: Int) = x + 1 val
Philip> addOneWithVal = (x: Int) => x + 1 }

Philip> The only difference is that you seem to tell addOneWithDef "*a
Philip> method*" and addOneWithVal "*a function value*".

Philip> But for me it seems to be or at least it feels like the same
Philip> thing... Is it about Java compatibility?

No. The distinction between methods and functions is fundamental to the
whole design of Scala. see
http://stackoverflow.com/questions/2529184/difference-between-method-and...

Raoul Duke
Joined: 2009-01-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Why is there a difference between def and val for methods

> Philip> Hello, why is there a difference between def and val for
> Philip> methods and functions?

please note, there are 2 issues here:

a) def vs. val

b) method vs. function

sincerely.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Why is there a difference between def and val for methods
There really isn't, aside from def being always recomputed, while val stores the result:   class MyScalaClass {
  def addOneWithDef = (x: Int) => x + 1
  val addOneWithVal = (x: Int) => x + 1
}

The other method syntax you shows, with addOneWithDef receiving a parameter, does not exist for val, as it would not be possible to store results in such case.   (though, that said, I'd love if "val" with parameters was a shortcut for memoization)
On Wed, Apr 7, 2010 at 4:54 PM, Philip May <eniak.info@googlemail.com> wrote:
Hello,

why is there a difference between def and val for methods and functions?

In this following example both methods / functions seem to do and seem to feel like the same...

class MyScalaClass {
  def addOneWithDef(x: Int) = x + 1
  val addOneWithVal = (x: Int) => x + 1
}

The only difference is that you seem to tell addOneWithDef "a method" and
addOneWithVal "a function value".

But for me it seems to be or at least it feels like the same thing...
Is it about Java compatibility?

Greetings,
Philip May

--
Philip May <eniak.info@googlemail.com>
Facebook: http://www.facebook.com/philip.may
Blog: http://blog.eniak.info - Twitter: http://twitter.com/pMay
Lifestream: http://friendfeed.com/pmay - Visitenkarte: http://eee.am/Eniak



--
Daniel C. Sobral

I travel to the future all the time.

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