- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why is there a difference between def and val for methods and functions?
Wed, 2010-04-07, 20:54
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
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
Wed, 2010-04-07, 21:47
#2
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.
Wed, 2010-04-07, 21:57
#3
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:
--
Daniel C. Sobral
I travel to the future all the time.
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.
>>>>> "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...