Quantcast

val, def, function, method

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

val, def, function, method

Raoul Duke
hi,

is there a concise, understandable, and correct explanation of how def
and val differ, and how they might be related to the distinction
between method & function in Scala?

thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Raoul Duke
ah, well, one part of the key i've found is, "However, only
definitions can take parameters; values must be constants."
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Kevin Wright-4
In reply to this post by Raoul Duke
You seem to have lots of concepts confused here...

a method is a member of a class, defined via the def keyword
a function is a first-class object. A method will be transparently promoted to a function if necessary.
You can also define a function by creating an object with an Apply method, or by using syntactic sugar (i.e `=>`)
a val is just a value, it can hold an object instance.  Remember that primitive types in scala are objects, as are functions.

2010/1/27 Raoul Duke <[hidden email]>
hi,

is there a concise, understandable, and correct explanation of how def
and val differ, and how they might be related to the distinction
between method & function in Scala?

thanks.



--
Kevin Wright

mail/google talk: [hidden email]
wave: [hidden email]
skype: kev.lee.wright
twitter: @thecoda

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Kevin Wright-4
actually, defs can also be nested inside other defs, they don't have to be direct class members.

2010/1/27 Kevin Wright <[hidden email]>
You seem to have lots of concepts confused here...

a method is a member of a class, defined via the def keyword
a function is a first-class object. A method will be transparently promoted to a function if necessary.
You can also define a function by creating an object with an Apply method, or by using syntactic sugar (i.e `=>`)
a val is just a value, it can hold an object instance.  Remember that primitive types in scala are objects, as are functions.

2010/1/27 Raoul Duke <[hidden email]>

hi,

is there a concise, understandable, and correct explanation of how def
and val differ, and how they might be related to the distinction
between method & function in Scala?

thanks.



--
Kevin Wright

mail/google talk: [hidden email]
wave: [hidden email]
skype: kev.lee.wright
twitter: @thecoda




--
Kevin Wright

mail/google talk: [hidden email]
wave: [hidden email]
skype: kev.lee.wright
twitter: @thecoda

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Daniel Sobral
In reply to this post by Raoul Duke
Please, let it be clear that the difference between "def" and "val" has nothing to do with functions.

There has been some flawed comparison like this:

def f(x: Int) = x * 2
val f = (x: Int) => x * 2

That's misguided! You could have the following just as well:

def f = (x: Int) => x * 2
val f = (x: Int) => x * 2

And then, what's the difference? So, ok, here is the difference:

* the expression assigned to a def will be executed every time the definition is invoked
* the expression assigned to a val will be executed once at declaration, and the result returned when invoked

Or, if this might make things simpler to understand to people coming from Java, when I declare "val f = something", I'm really creating a field called "f", assigning "something" to it, and also creating a getter method called "f", which returns the value of the field "f". 

Declaring "def f", on the other hand, simply created a method which returns the "something" expression.

Note that one can even do this:

class X {
  def f = (x: Int) => x * 2
}

class Y extends X {
  override val f = (x: Int) = x * 4
}

The other way is not possible, because "val" guarantees the result won't change, nor will there be any side effects, so if one went from "val" to "def", it would break that contract.

Now, "def" may also receive parameters, which a "val" can't. Since the result of a "val" isn't supposed to change, it wouldn't make any sense for it to receive a parameter.

On Wed, Jan 27, 2010 at 8:31 PM, Raoul Duke <[hidden email]> wrote:
hi,

is there a concise, understandable, and correct explanation of how def
and val differ, and how they might be related to the distinction
between method & function in Scala?

thanks.



--
Daniel C. Sobral

I travel to the future all the time.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Raoul Duke
hi

On Wed, Jan 27, 2010 at 5:03 PM, Daniel Sobral <[hidden email]> wrote:
> Please, let it be clear that the difference between "def" and "val" has
> nothing to do with functions.

many thanks for the comments, they are helping! i think. :-)

question: how should one choose between using def vs. val?

thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Raoul Duke
On Wed, Jan 27, 2010 at 5:12 PM, Raoul Duke <[hidden email]> wrote:
> question: how should one choose between using def vs. val?

p.s. i mean explicitly for functions/methods.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Fwd: val, def, function, method

Raoul Duke
In reply to this post by Daniel Sobral
er, meant to reply to the list.


---------- Forwarded message ----------
From: Raoul Duke <[hidden email]>
Date: Wed, Jan 27, 2010 at 5:15 PM
Subject: Re: [scala-user] val, def, function, method
To: Daniel Sobral <[hidden email]>


hi.

On Wed, Jan 27, 2010 at 5:03 PM, Daniel Sobral <[hidden email]> wrote:
> Please, let it be clear that the difference between "def" and "val" has
> nothing to do with functions.

then what is all the hullabaloo about being able to convert a method
to a function? i mean, what is the right way to understand that topic?
(i will try to experiment with it in the repl...)

thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Naftoli Gugenheim
In reply to this post by Raoul Duke
In a way #1 is more similar to #2. Only in #3 are new Function1 instances generated every time it's invoked. Other than in a class body, I believe #1 is syntactic sugar for #2.

-------------------------------------
Daniel Sobral<[hidden email]> wrote:

Please, let it be clear that the difference between "def" and "val" has
nothing to do with functions.

There has been some flawed comparison like this:

def f(x: Int) = x * 2
val f = (x: Int) => x * 2

That's misguided! You could have the following just as well:

def f = (x: Int) => x * 2
val f = (x: Int) => x * 2

And then, what's the difference? So, ok, here is the difference:

* the expression assigned to a def will be executed every time the
definition is invoked
* the expression assigned to a val will be executed once at declaration, and
the result returned when invoked

Or, if this might make things simpler to understand to people coming from
Java, when I declare "val f = something", I'm really creating a field called
"f", assigning "something" to it, and also creating a getter method called
"f", which returns the value of the field "f".

Declaring "def f", on the other hand, simply created a method which returns
the "something" expression.

Note that one can even do this:

class X {
  def f = (x: Int) => x * 2
}

class Y extends X {
  override val f = (x: Int) = x * 4
}

The other way is not possible, because "val" guarantees the result won't
change, nor will there be any side effects, so if one went from "val" to
"def", it would break that contract.

Now, "def" may also receive parameters, which a "val" can't. Since the
result of a "val" isn't supposed to change, it wouldn't make any sense for
it to receive a parameter.

On Wed, Jan 27, 2010 at 8:31 PM, Raoul Duke <[hidden email]> wrote:

> hi,
>
> is there a concise, understandable, and correct explanation of how def
> and val differ, and how they might be related to the distinction
> between method & function in Scala?
>
> thanks.
>



--
Daniel C. Sobral

I travel to the future all the time.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Rex Kerr-2
In reply to this post by Raoul Duke
---------- Forwarded message ----------
From: Raoul Duke <[hidden email]>

On Wed, Jan 27, 2010 at 5:03 PM, Daniel Sobral <[hidden email]> wrote:
> Please, let it be clear that the difference between "def" and "val" has
> nothing to do with functions.

then what is all the hullabaloo about being able to convert a method
to a function?

Methods take parameters as arguments, including functions.  Methods _don't_ take methods as arguments.

Being able to convert a method to a function is a great way to get around that limitation.

Let's look at a few cases:

def f(i: Int) = i*2
// Java equivalent would be public int f(int i) { return i*2; }

This is a method takes an integer, multiplies it by two, and returns the resulting integer.  We're using "def" because that's the syntax to create a new method.

val g = (i: Int) => i*2
val g = f _
val g = new Function1[Int,Int] { def apply(i: Int) = i*2 }

These guys create functions and assign them to an immutable variable so we can refer to them later.  In one case, the function is specified directly.  In the second, it's generated from a method.  In the third case, we create the same function, but long-hand so we can see that a function is really just an instance of a particular class with the code in question in a method called "apply".  Now, let's use these guys:

f(5)  // Calls the method with 5, returning 10
g(5) // Calls the function saved in g with 5, returning 10
g.apply(5)  // Long-hand for g(5)

But we don't have to store a function; instead, we could

def h = (i: Int) => i*2

Now we have a method that returns a function; the compiler will typically create the function object, and then assign "h" as a getter of that (secretly stored) object.  So,

h(5)

works too, but if it were Java would look something like

h().apply(5)

because it's a method call to get the function object, followed by the apply method called on that function object.  Anyway, suppose we want to create a method that applies an arbitrary transformation on integers:

def transform(i: Int, a: Int => Int) = a(i)

Now we might try applying this:

transform(5, g)   // Works fine!  g holds a function object of the right type
transform(5, h)   //  Works fine!  h is a getter for a function object (h returns a function object)
transform(5, f)   // Does NOT work--f is a method, not a function!
transform(5, f _)  // Works--we convert f into a function right here

So converting methods to functions allows you to toss methods (or things that act like them) around whenever you feel like it--you can pass not just objects (bundles of data that know how to operate on themselves with methods), but functions (knowledge of how to do some operation, with just enough data carried along to make it work).

Does this help?

  --Rex

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

James Iry-2
In reply to this post by Raoul Duke
Raoul,

I can't help but think you're making this more complex than it needs to be


val x : A = whatever

is exactly the same as Java's

final A x = whatever;



Scala's

var x : A = whatever

is exactly the same as Java's

A x = whatever;


Scala's

def foo[T](x : T) : B = whatever

is exactly the same as Java's

public <T> B whatever(T x) { return whatever; }

Except that defs can be nested, while Java doesn't allow that.



Scala's

{x  => whatever} : (A => B)

is exactly the same as Java's

new Function1<A, B> {
   B apply(A x) { return whatever;}
}


Now, Scala allows "promotion" of a method to a function object

List(1,2,3) foreach println

means

List(1,2,3).foreach{x => println(x)}

means

List(1,2,3).foreach(new Function1[Int, Unit] { def apply(x : Int) = println(x) })


And of course, Scala allows many of the types I've explicitly annotated here to be dropped.


On Wed, Jan 27, 2010 at 5:16 PM, Raoul Duke <[hidden email]> wrote:
er, meant to reply to the list.


---------- Forwarded message ----------
From: Raoul Duke <[hidden email]>
Date: Wed, Jan 27, 2010 at 5:15 PM
Subject: Re: [scala-user] val, def, function, method
To: Daniel Sobral <[hidden email]>


hi.

On Wed, Jan 27, 2010 at 5:03 PM, Daniel Sobral <[hidden email]> wrote:
> Please, let it be clear that the difference between "def" and "val" has
> nothing to do with functions.

then what is all the hullabaloo about being able to convert a method
to a function? i mean, what is the right way to understand that topic?
(i will try to experiment with it in the repl...)

thanks.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Raoul Duke
On Thu, Jan 28, 2010 at 10:28 AM, James Iry <[hidden email]> wrote:
> Raoul,
> I can't help but think you're making this more complex than it needs to be

lordy, i sure hope so! seriously.

i will read what you wrote and ponder.

thanks to all.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Daniel Sobral
In reply to this post by James Iry-2
James, is he? That depends on the context, doesn't it?
 
Suppose you want to use Scala as a language for some grad course, before (or without) introducing Java. How would you explain def&val, then? It must be possible to explain them without resorting to "look, this is how it is implemented in Java".
 
And this is made rather more difficult by some saying that "def" declares a function, and that "val" may be a function too.

On Thu, Jan 28, 2010 at 4:28 PM, James Iry <[hidden email]> wrote:
Raoul,

I can't help but think you're making this more complex than it needs to be


val x : A = whatever

is exactly the same as Java's

final A x = whatever;



Scala's

var x : A = whatever

is exactly the same as Java's

A x = whatever;


Scala's

def foo[T](x : T) : B = whatever

is exactly the same as Java's

public <T> B whatever(T x) { return whatever; }

Except that defs can be nested, while Java doesn't allow that.



Scala's

{x  => whatever} : (A => B)

is exactly the same as Java's

new Function1<A, B> {
   B apply(A x) { return whatever;}
}


Now, Scala allows "promotion" of a method to a function object

List(1,2,3) foreach println

means

List(1,2,3).foreach{x => println(x)}

means

List(1,2,3).foreach(new Function1[Int, Unit] { def apply(x : Int) = println(x) })


And of course, Scala allows many of the types I've explicitly annotated here to be dropped.



On Wed, Jan 27, 2010 at 5:16 PM, Raoul Duke <[hidden email]> wrote:
er, meant to reply to the list.


---------- Forwarded message ----------
From: Raoul Duke <[hidden email]>
Date: Wed, Jan 27, 2010 at 5:15 PM
Subject: Re: [scala-user] val, def, function, method
To: Daniel Sobral <[hidden email]>


hi.

On Wed, Jan 27, 2010 at 5:03 PM, Daniel Sobral <[hidden email]> wrote:
> Please, let it be clear that the difference between "def" and "val" has
> nothing to do with functions.

then what is all the hullabaloo about being able to convert a method
to a function? i mean, what is the right way to understand that topic?
(i will try to experiment with it in the repl...)

thanks.




--
Daniel C. Sobral

I travel to the future all the time.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: val, def, function, method

Stefan Langer-4
I think part of the problem is that val is compared to java types and not on itself.
If the people have no idea about Java or programming in general then simply state that 
val defines a value which may not be reassigned once defined.
def is a definition and can be a function or a method depending on its context. Where a method can be converted to a function where nessecary.
If they have never had any experience with other OO Languages then this should be enough to get them started.
The confusion arises when compared to other OO Languages especially Java as it does not offer the same semantics.

- Stefan

2010/1/28 Daniel Sobral <[hidden email]>
James, is he? That depends on the context, doesn't it?
 
Suppose you want to use Scala as a language for some grad course, before (or without) introducing Java. How would you explain def&val, then? It must be possible to explain them without resorting to "look, this is how it is implemented in Java".
 
And this is made rather more difficult by some saying that "def" declares a function, and that "val" may be a function too.

Loading...