• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

def vs val

 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it matter whether I use def or val in these functions? Both seem to do the same thing.



 
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

     
    Mohamed Sanaulla
    Bartender
    Posts: 3225
    34
    IntelliJ IDE Oracle Spring Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I was waiting for Jesper to answer this Thanks a lot for the explanation.
     
    Samuel Cox
    Ranch Hand
    Posts: 96
    Scala VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Ranch Hand
    Posts: 72
    Scala Monad Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Luigi Plinge
    Ranch Hand
    Posts: 441
    Scala IntelliJ IDE Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the explanations.

    The link Samuel Cox gives above is also quite helpful.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic