I think the difference is only true in a class body. Inside a code block I 
think the def is syntactic sugar for the other syntax.

-------------------------------------
Viktor Klang<viktor.kl...@gmail.com> wrote:

On Thu, Jul 30, 2009 at 11:02 PM, DFectuoso <santiago1...@gmail.com> wrote:

>
> Viktor, that was a great explanation, im sure ben will apraciate that
> kind of teaching =)


Well, thank you.


>
>
> A follow up question that just made me wonder, what is the diference
> between
>
> val square = (x : Int) => x * x
>

This creates a final reference to a new function instance


>
> and
> def square(x:Int):Int = x * x
>

This creates a method

Now, you can transform a method into a function by adding an underscore
after it, like this:

def square(x : Int) = x * x

val sq = square _ //<--- notice the underscore. (I always think of it as
"detaching" the method from an instance)

This is what the Scala REPL says:

scala> def square(x : Int) = x * x
square: (Int)Int

scala> val vsquare = (x : Int) => x * x
vsquare: (Int) => Int = <function>

scala> val v2square = square _
v2square: (Int) => Int = <function>

scala> square(2)
res0: Int = 4

scala> vsquare(2)
res1: Int = 4

scala> v2square(2)
res2: Int = 4





>
> ?
>
>
> On Jul 30, 1:49 pm, Viktor Klang <viktor.kl...@gmail.com> wrote:
> > Hello Ben!
> >
> > the following:
> >
> > val square = (x:Int) => x * x
> >
> > "val square" is the equivalent of a "final" reference in Java.
> >
> > (x : Int) => x * x
> >
> > This constructs a Function instance that takes an Int and returns that
> Int
> > squared.
> >
> > The equivalent Java code would be something like:
> >
> > interface Function<R,P> //We're ignoring variance here, as to not confuse
> > {
> >     public R call(P p);
> >
> > }
> >
> > final Function<Integer,Integer> square = new Function<Integer,Integer>()
> {
> >      public Integer call(Integer i)
> >      {
> >          return i * i;
> >      }
> >
> > }
> >
> > And then you can do:
> >
> > square.call(2) // 4
> >
> > So, back to Scala:
> >
> > val square = (x : Int) => x * x
> >
> > square(2) // 4
> >
> >
> >
> > On Thu, Jul 30, 2009 at 9:37 PM, ben <b...@primrose.org.uk> wrote:
> >
> > > Hi,
> >
> > > I feel I must apologise for this  post ... I've only been doing scala
> > > for a week, and lift for 3 days, but I'm stuck.
> > > I'm coming from an experienced java background, just struggling with
> > > some scala constructs.
> >
> > > I feel I understand the basics of lambda and function literals - but
> > > I'm blown away by some of the syntax in the lift "Getting Started"
> > > tutorial.
> > > For example, given :
> >
> > > val mylist = Array(1,2,3)
> > > mylist..foreach(v => println(v)
> >
> > > Here I understand where "v" comes from - its each int in the list.
> >
> > > In the tutorial (http://liftweb.net/docs/getting_started/
> > > mod_master.html<
> http://liftweb.net/docs/getting_started/%0Amod_master.html>
> > > )
> > > there is a function "desc" (Listing 15) :
> >
> > > private def desc(td: ToDo, reDraw: () => JsCmd) =
> > >  swappable(<span>{td.desc}</span>,
> > >         <span>{ajaxText(td.desc,
> > >                     v => {td.desc(v).save; reDraw()})}
> > >         </span>)
> >
> > > For my own brain to try and "break it down", I have :
> >
> > >  private def desc(td: ToDo, reDraw: () => JsCmd) = {
> > >    val myFunctionLiteral = (xxx: String) => {td.desc(xxx).save;
> > > println("!!!!!!!!!Desc function :" + xxx); reDraw()}
> >
> > >    swappable(<span>{td.desc}</span>,
> > >      <span>{ajaxText(td.desc, myFunctionLiteral)}</span>)
> > >  }
> >
> > > This is called from the "doList" method :
> >
> > > private def doList(reDraw: () => JsCmd)(html: NodeSeq): NodeSeq =
> > >  toShow.
> > >  flatMap(td =>
> > >   bind("todo", html,
> > >      "desc" -> desc(td, reDraw)
> > >   ))
> >
> > > I understand where the "ToDo" object "td" is coming from, and how the
> > > "reDraw" thing works, and how they are passed to the "desc" function,
> > > but what I don't understand at all is where the val "xxx" in
> > > "myFunctionLiteral" comes from ?
> > > How is it passed into the function ? Is it currying ?
> >
> > > I'm sorry for such a vague question, I'm totally at sea here.
> >
> > > Thankyou for reading this, and for any help you may provide !
> >
> > > Ben
> >
> > --
> > Viktor Klang
> >
> > Rogue Scala-head
> >
> > Blog: klangism.blogspot.com
> > Twttr: viktorklang
> >
>


-- 
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to