[Lift] Re: Newbie: zero parameter functions, bind call-by-name function curiosity
On Oct 27, 8:50 pm, Strom wrote: > Yes Ross, I did mean () => processForm. Thanks for the clarification. > The one thing in moving from Java to Scala is that there are a lot of > things that look similar that aren't, as you've pointed out. Newbie > error caught ;) > > Thanks Marius, I made that assumption, but it still bugs me that the > scala interpreter evaluates both the "value" and the "function" when I > use: > > scala> def testFunc() : Int = 3 > > scala> testFunc > Int = 3 > > scala>testFunc() > Int = 3 Right ... and this is correct. Because there is no context the interpreter (and compiler) assume that you mean a function- application. But try this: scala> testFunc _ res5: () => Int = the _ here means eta expansion. A method is converted into a function. SLS 6.25.5 But context matters: scala> def testFunc(): Int = 3 testFunc: ()Int scala> def other(f: () => Int) = f() other: (() => Int)Int scala> other(testFunc) res0: Int = 3 scala> other(testFunc()) :7: error: type mismatch; found : Int required: () => Int other(testFunc()) ^ In the above example textFunc and testFunc() means two different things > > Strom > > On Oct 27, 7:19 am, Ross Mellgren wrote: > > > Did you mean () => processForm by chance? () -> processForm is a pair > > of () and processForm's result. > > > -Ross > > > On Oct 27, 2009, at 2:52 AM, Strom wrote: > > > > I'm not sure if this is a scala or a lift related question, but when I > > > try to bind a submit button, I'm having some confusion on using zero > > > parameter functions as the "() -> Any" parameter in the submit > > > signature. > > > > The signature for the method in question is > > > SHtml.submit(value : String, func : () => Any, attrs : (String, > > > String) > > > *) > > > > 1. My first question is about the following method definitions > > > def processForm () : Unit = {...} > > > vs > > > def processForm = {...} //no parentheses after method declaration > > > > Why can I call "processForm" as well as "processForm()" for the first > > > example, but only "processForm" for the second example. If there is > > > scala/lift documentation on this simple question? > > > > 2. Why do I get a compiler error when I use the following code: > > > bind( ... > > > "submit" -> SHtml.submit( "Process Form", processForm() ) /*type > > > mismatch error*/ > > > ) > > > vs > > > bind( ... > > > "submit" -> SHtml.submit( "Process Form", processForm ) /*compiles > > > successfully*/ > > > ) > > > > It's my impression that the above bind calls are equivalent given the > > > way the scala compiler behaves when I call "processForm" or > > > "processForm()". > > > > Also, I wasn't even sure the method itself would allow the code to > > > compile. I thought I'd have to do "() -> processForm" or "() -> > > > processForm()". > > > > The error I was getting was this: > > > error type mismatch > > > found : Unit > > > required : () -> Any > > > > Can someone please clarify? Thanks so much. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[Lift] Re: Newbie: zero parameter functions, bind call-by-name function curiosity
Yes Ross, I did mean () => processForm. Thanks for the clarification. The one thing in moving from Java to Scala is that there are a lot of things that look similar that aren't, as you've pointed out. Newbie error caught ;) Thanks Marius, I made that assumption, but it still bugs me that the scala interpreter evaluates both the "value" and the "function" when I use: scala> def testFunc() : Int = 3 scala> testFunc Int = 3 scala>testFunc() Int = 3 Strom On Oct 27, 7:19 am, Ross Mellgren wrote: > Did you mean () => processForm by chance? () -> processForm is a pair > of () and processForm's result. > > -Ross > > On Oct 27, 2009, at 2:52 AM, Strom wrote: > > > > > I'm not sure if this is a scala or a lift related question, but when I > > try to bind a submit button, I'm having some confusion on using zero > > parameter functions as the "() -> Any" parameter in the submit > > signature. > > > The signature for the method in question is > > SHtml.submit(value : String, func : () => Any, attrs : (String, > > String) > > *) > > > 1. My first question is about the following method definitions > > def processForm () : Unit = {...} > > vs > > def processForm = {...} //no parentheses after method declaration > > > Why can I call "processForm" as well as "processForm()" for the first > > example, but only "processForm" for the second example. If there is > > scala/lift documentation on this simple question? > > > 2. Why do I get a compiler error when I use the following code: > > bind( ... > > "submit" -> SHtml.submit( "Process Form", processForm() ) /*type > > mismatch error*/ > > ) > > vs > > bind( ... > > "submit" -> SHtml.submit( "Process Form", processForm ) /*compiles > > successfully*/ > > ) > > > It's my impression that the above bind calls are equivalent given the > > way the scala compiler behaves when I call "processForm" or > > "processForm()". > > > Also, I wasn't even sure the method itself would allow the code to > > compile. I thought I'd have to do "() -> processForm" or "() -> > > processForm()". > > > The error I was getting was this: > > error type mismatch > > found : Unit > > required : () -> Any > > > Can someone please clarify? Thanks so much. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[Lift] Re: Newbie: zero parameter functions, bind call-by-name function curiosity
Did you mean () => processForm by chance? () -> processForm is a pair of () and processForm's result. -Ross On Oct 27, 2009, at 2:52 AM, Strom wrote: > > I'm not sure if this is a scala or a lift related question, but when I > try to bind a submit button, I'm having some confusion on using zero > parameter functions as the "() -> Any" parameter in the submit > signature. > > The signature for the method in question is > SHtml.submit(value : String, func : () => Any, attrs : (String, > String) > *) > > 1. My first question is about the following method definitions > def processForm () : Unit = {...} > vs > def processForm = {...} //no parentheses after method declaration > > Why can I call "processForm" as well as "processForm()" for the first > example, but only "processForm" for the second example. If there is > scala/lift documentation on this simple question? > > 2. Why do I get a compiler error when I use the following code: > bind( ... > "submit" -> SHtml.submit( "Process Form", processForm() ) /*type > mismatch error*/ > ) > vs > bind( ... > "submit" -> SHtml.submit( "Process Form", processForm ) /*compiles > successfully*/ > ) > > It's my impression that the above bind calls are equivalent given the > way the scala compiler behaves when I call "processForm" or > "processForm()". > > Also, I wasn't even sure the method itself would allow the code to > compile. I thought I'd have to do "() -> processForm" or "() -> > processForm()". > > > The error I was getting was this: > error type mismatch > found : Unit > required : () -> Any > > Can someone please clarify? Thanks so much. > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[Lift] Re: Newbie: zero parameter functions, bind call-by-name function curiosity
On Oct 27, 8:52 am, Strom wrote: > I'm not sure if this is a scala or a lift related question, but when I > try to bind a submit button, I'm having some confusion on using zero > parameter functions as the "() -> Any" parameter in the submit > signature. > > The signature for the method in question is > SHtml.submit(value : String, func : () => Any, attrs : (String, String) > *) > > 1. My first question is about the following method definitions > def processForm () : Unit = {...} > vs > def processForm = {...} //no parentheses after method declaration > > Why can I call "processForm" as well as "processForm()" for the first > example, but only "processForm" for the second example. If there is > scala/lift documentation on this simple question? Because compiler already knows that processForm is a zero argument function and accepts processForm call. In the second example you just can't call a call-by-name using () as it would contradict its very definition. > > 2. Why do I get a compiler error when I use the following code: > bind( ... > "submit" -> SHtml.submit( "Process Form", processForm() ) /*type > mismatch error*/ > ) > vs > bind( ... > "submit" -> SHtml.submit( "Process Form", processForm ) /*compiles > successfully*/ > ) processForm() is a value not a function. It is the value returned by processForm function while processForm is the function itself. > > It's my impression that the above bind calls are equivalent given the > way the scala compiler behaves when I call "processForm" or > "processForm()". > > Also, I wasn't even sure the method itself would allow the code to > compile. I thought I'd have to do "() -> processForm" or "() -> > processForm()". > > The error I was getting was this: > error type mismatch > found : Unit > required : () -> Any > > Can someone please clarify? Thanks so much. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---