aspose file tools*
The moose likes Scala and the fly likes def vs val Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Scala
Bookmark "def vs val" Watch "def vs val" New topic
Author

def vs val

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Does it matter whether I use def or val in these functions? Both seem to do the same thing.



Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

I don't think it matters. My guess is one is syntactic sugar for the other. I'll further guess def is the sugar...

I think most people use the def keyword to define functions unless they are anonymous.

Here are the anonymous function versions:




Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Thanks, very helpful.

I note we can also use both of the following:

which both seem to do the same thing, although the REPL gives slightly diffrent messages after you type each in.

I don't think it's just syntactic sugar because if you just type in the name of the function in each case, it comes back with:

scala> greater
<console>:8: error: missing arguments for method greater in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
greater
^

scala> greater2
res31: (Int, List[Int]) => List[Int] = <function2>


So is one being treated as a method and the other a function?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

In the above example- greater is a function which has to be invoked with the parameters, where as greater2 is a function value- A closure?

Am just not sure about this though.


Mohamed Sanaulla | My Blog
Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

I'm glad that I specified I was guessing:)

I think both constructs can classify as closures, since both are essentially creating functions that capture their surrounding scope.



http://jim-mcbeath.blogspot.com/2009/05/scala-functions-vs-methods.html has some info on it, but I don't think it is a 100% match to your question. You might try the mailing list if you can't get a satisfactory answer (but please come back;).
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14103
    
  16


def and val are two different things.

  • With def, you declare a method. The expression won't be evaluated until you call the method.
  • With val, you create a value (a constant). The expression is evaluated at the point where you declare it.


  • But you've written some code that makes it a bit confusing.

    Note that the val fun in your method greater2 is a function: it has the type Int => Boolean. The syntax:

    is syntactic sugar for this:

    So, in that case, fun is really a Function1 object with an apply method that gets called when you make a "call" on fun.

    Note by the way that an expression like if(x > n) true else false is overly verbose. You can just write x > n, which is by itself an expression that evaluates to a Boolean:


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
    Mohamed Sanaulla
    Saloon Keeper

    Joined: Sep 08, 2007
    Posts: 3068
        
      33

    I was waiting for Jesper to answer this Thanks a lot for the explanation.
    Samuel Cox
    Ranch Hand

    Joined: Nov 16, 2004
    Posts: 96

    Ok, I stand corrected.

    Furthermore, I just had a colleague tell me that my def b() = i + 1 was not technically a closure; whereas, val b = () => i + 1 is.

    Clearly, I have a lot to learn!

    The good news is you can do a lot of good stuff with Scala without understanding these finer points. At least, that's my opinion.
    Marimuthu Madasamy
    Ranch Hand

    Joined: Jun 07, 2007
    Posts: 72

    To add, With 'def' you are creating a method which means you cannot pass it around as first class value whereas 'val's containing functions are first class values so you can pass it around.

    Thats why you are getting the following error since the method is not passed enough arguments hence it is not a method call and also it cannot be assigned to the REPL generated variable since it is not a first class value:



    If we follow this method with '_', a function is created which can be passed around/assigned to a variable.


    - Marimuthu Madasamy
    Luigi Plinge
    Ranch Hand

    Joined: Jan 06, 2011
    Posts: 441

    Thanks for the explanations.

    The link Samuel Cox gives above is also quite helpful.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: def vs val