- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
"inconsistencies" in function syntax
Tue, 2009-07-21, 15:08
I find that, on occasion, the syntax constructs in Scala surprise me, and I have a hard time deconstructing why they are the way they are.
For example:
class Person(first:String, last:String) {
def name(printer:(String,String) => String) = printer(first,last)
}
object FunctionDemo {
def main(args: Array[String]) {
val me = new Person("Dave","Copeland")
def casual(first:String,last:String) = first + " " + last
println(me.name((f,l) => f.toUpperCase + "_" + l.toUpperCase))
println(me.name(casual))
}
}
In the first println, I create an anonymous function. What I find strange is the "=>". When I define a non-anonymous function/method (as in "def casual"), I use the equals sign (in fact, it is a syntax error to use the arrow). On its face, this seems like rather arbitrary syntax. Compound this with the fact that, when referring to a function type, you use the arrow to indicate return value (and not a colon, as in non-function types), and I feel like I'm just memorize arbitrary syntax rules that don't make much sense.
As an example, the following feels more consistent to me:
class Person(first:String, last:String) {
def name(printer:(String,String): String) = printer(first,last)
}
object FunctionDemo {
def main(args: Array[String]) {
val me = new Person("Dave","Copeland")
def casual(first:String,last:String) = first + " " + last
println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
println(me.name(casual))
}
}
So, my question is why is the syntax this way? How can I mentally decompose the syntactical elements in a way that makes sense.
Dave
For example:
class Person(first:String, last:String) {
def name(printer:(String,String) => String) = printer(first,last)
}
object FunctionDemo {
def main(args: Array[String]) {
val me = new Person("Dave","Copeland")
def casual(first:String,last:String) = first + " " + last
println(me.name((f,l) => f.toUpperCase + "_" + l.toUpperCase))
println(me.name(casual))
}
}
In the first println, I create an anonymous function. What I find strange is the "=>". When I define a non-anonymous function/method (as in "def casual"), I use the equals sign (in fact, it is a syntax error to use the arrow). On its face, this seems like rather arbitrary syntax. Compound this with the fact that, when referring to a function type, you use the arrow to indicate return value (and not a colon, as in non-function types), and I feel like I'm just memorize arbitrary syntax rules that don't make much sense.
As an example, the following feels more consistent to me:
class Person(first:String, last:String) {
def name(printer:(String,String): String) = printer(first,last)
}
object FunctionDemo {
def main(args: Array[String]) {
val me = new Person("Dave","Copeland")
def casual(first:String,last:String) = first + " " + last
println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
println(me.name(casual))
}
}
So, my question is why is the syntax this way? How can I mentally decompose the syntactical elements in a way that makes sense.
Dave
Tue, 2009-07-21, 15:37
#2
Re: "inconsistencies" in function syntax
Then what about
def casual(first:String,last:String) => first + " " + last
And why is the return type of a function followed by an arrow and not a colon like everything else?
Dave
On Tue, Jul 21, 2009 at 10:19 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
def casual(first:String,last:String) => first + " " + last
And why is the return type of a function followed by an arrow and not a colon like everything else?
Dave
On Tue, Jul 21, 2009 at 10:19 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
> class Person(first:String, last:String) {
> def name(printer:(String,String): String) = printer(first,last)
> }
How about: printer:String:String? Does this look.. consistent?
> println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
That looks to me like you are assigning f.toUpperCase + "_" +
l.toUpperCase to f and l, like this statement:
val (x, y) = (1, 2)
Scala's => is from the lambda syntax (I'll use Haskell syntax because
I don't have my Greek with me): \x -> x * 2
C# does it the same way, F# uses ->, the BGGA proposal for adding
lambdas to Java does it the same way. I'd rather not have to look
carefully to tell whether an = is part of a lambda or a side effect.
2009/7/21 David Copeland <davec@naildrivin5.com>:
> I find that, on occasion, the syntax constructs in Scala surprise me, and I
> have a hard time deconstructing why they are the way they are.
>
> For example:
>
> class Person(first:String, last:String) {
> def name(printer:(String,String) => String) = printer(first,last)
> }
>
> object FunctionDemo {
> def main(args: Array[String]) {
> val me = new Person("Dave","Copeland")
>
> def casual(first:String,last:String) = first + " " + last
>
> println(me.name((f,l) => f.toUpperCase + "_" + l.toUpperCase))
> println(me.name(casual))
> }
> }
>
> In the first println, I create an anonymous function. What I find strange
> is the "=>". When I define a non-anonymous function/method (as in "def
> casual"), I use the equals sign (in fact, it is a syntax error to use the
> arrow). On its face, this seems like rather arbitrary syntax. Compound
> this with the fact that, when referring to a function type, you use the
> arrow to indicate return value (and not a colon, as in non-function types),
> and I feel like I'm just memorize arbitrary syntax rules that don't make
> much sense.
>
> As an example, the following feels more consistent to me:
>
> class Person(first:String, last:String) {
> def name(printer:(String,String): String) = printer(first,last)
> }
>
> object FunctionDemo {
> def main(args: Array[String]) {
> val me = new Person("Dave","Copeland")
>
> def casual(first:String,last:String) = first + " " + last
>
> println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
> println(me.name(casual))
> }
> }
>
> So, my question is why is the syntax this way? How can I mentally decompose
> the syntactical elements in a way that makes sense.
>
> Dave
>
--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@gmail.com
Tue, 2009-07-21, 15:37
#3
Re: "inconsistencies" in function syntax
Would you do the same for a val?
val x:Int:Int => (y: Int) => y * 2
At any rate, Scala's not going to change at this level.
2009/7/21 David Copeland :
> Then what about
>
> def casual(first:String,last:String) => first + " " + last
>
> And why is the return type of a function followed by an arrow and not a
> colon like everything else?
>
> Dave
>
> On Tue, Jul 21, 2009 at 10:19 AM, Ricky Clarkson
> wrote:
>>
>> > class Person(first:String, last:String) {
>> > def name(printer:(String,String): String) = printer(first,last)
>> > }
>>
>> How about: printer:String:String? Does this look.. consistent?
>>
>> > println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
>>
>> That looks to me like you are assigning f.toUpperCase + "_" +
>> l.toUpperCase to f and l, like this statement:
>> val (x, y) = (1, 2)
>>
>> Scala's => is from the lambda syntax (I'll use Haskell syntax because
>> I don't have my Greek with me): \x -> x * 2
>>
>> C# does it the same way, F# uses ->, the BGGA proposal for adding
>> lambdas to Java does it the same way. I'd rather not have to look
>> carefully to tell whether an = is part of a lambda or a side effect.
>>
>> 2009/7/21 David Copeland :
>> > I find that, on occasion, the syntax constructs in Scala surprise me,
>> > and I
>> > have a hard time deconstructing why they are the way they are.
>> >
>> > For example:
>> >
>> > class Person(first:String, last:String) {
>> > def name(printer:(String,String) => String) = printer(first,last)
>> > }
>> >
>> > object FunctionDemo {
>> > def main(args: Array[String]) {
>> > val me = new Person("Dave","Copeland")
>> >
>> > def casual(first:String,last:String) = first + " " + last
>> >
>> > println(me.name((f,l) => f.toUpperCase + "_" + l.toUpperCase))
>> > println(me.name(casual))
>> > }
>> > }
>> >
>> > In the first println, I create an anonymous function. What I find
>> > strange
>> > is the "=>". When I define a non-anonymous function/method (as in "def
>> > casual"), I use the equals sign (in fact, it is a syntax error to use
>> > the
>> > arrow). On its face, this seems like rather arbitrary syntax. Compound
>> > this with the fact that, when referring to a function type, you use the
>> > arrow to indicate return value (and not a colon, as in non-function
>> > types),
>> > and I feel like I'm just memorize arbitrary syntax rules that don't make
>> > much sense.
>> >
>> > As an example, the following feels more consistent to me:
>> >
>> > class Person(first:String, last:String) {
>> > def name(printer:(String,String): String) = printer(first,last)
>> > }
>> >
>> > object FunctionDemo {
>> > def main(args: Array[String]) {
>> > val me = new Person("Dave","Copeland")
>> >
>> > def casual(first:String,last:String) = first + " " + last
>> >
>> > println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
>> > println(me.name(casual))
>> > }
>> > }
>> >
>> > So, my question is why is the syntax this way? How can I mentally
>> > decompose
>> > the syntactical elements in a way that makes sense.
>> >
>> > Dave
>> >
>>
>>
>>
>> --
>> Ricky Clarkson
>> Java Programmer, AD Holdings
>> +44 1565 770804
>> Skype: ricky_clarkson
>> Google Talk: ricky.clarkson@gmail.com
>
>
Tue, 2009-07-21, 15:57
#4
Re: "inconsistencies" in function syntax
String => Int
is a shortcut notation for
Function2[String, Int]
Am I right in assuming that you come from a functional programming background, so some of the object stuff might not make much sense to you at first
On Tue, Jul 21, 2009 at 3:26 PM, David Copeland <davetron5000@gmail.com> wrote:
is a shortcut notation for
Function2[String, Int]
Am I right in assuming that you come from a functional programming background, so some of the object stuff might not make much sense to you at first
On Tue, Jul 21, 2009 at 3:26 PM, David Copeland <davetron5000@gmail.com> wrote:
Then what about
def casual(first:String,last:String) => first + " " + last
And why is the return type of a function followed by an arrow and not a colon like everything else?
Dave
On Tue, Jul 21, 2009 at 10:19 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
> class Person(first:String, last:String) {
> def name(printer:(String,String): String) = printer(first,last)
> }
How about: printer:String:String? Does this look.. consistent?
> println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
That looks to me like you are assigning f.toUpperCase + "_" +
l.toUpperCase to f and l, like this statement:
val (x, y) = (1, 2)
Scala's => is from the lambda syntax (I'll use Haskell syntax because
I don't have my Greek with me): \x -> x * 2
C# does it the same way, F# uses ->, the BGGA proposal for adding
lambdas to Java does it the same way. I'd rather not have to look
carefully to tell whether an = is part of a lambda or a side effect.
2009/7/21 David Copeland <davec@naildrivin5.com>:
> I find that, on occasion, the syntax constructs in Scala surprise me, and I
> have a hard time deconstructing why they are the way they are.
>
> For example:
>
> class Person(first:String, last:String) {
> def name(printer:(String,String) => String) = printer(first,last)
> }
>
> object FunctionDemo {
> def main(args: Array[String]) {
> val me = new Person("Dave","Copeland")
>
> def casual(first:String,last:String) = first + " " + last
>
> println(me.name((f,l) => f.toUpperCase + "_" + l.toUpperCase))
> println(me.name(casual))
> }
> }
>
> In the first println, I create an anonymous function. What I find strange
> is the "=>". When I define a non-anonymous function/method (as in "def
> casual"), I use the equals sign (in fact, it is a syntax error to use the
> arrow). On its face, this seems like rather arbitrary syntax. Compound
> this with the fact that, when referring to a function type, you use the
> arrow to indicate return value (and not a colon, as in non-function types),
> and I feel like I'm just memorize arbitrary syntax rules that don't make
> much sense.
>
> As an example, the following feels more consistent to me:
>
> class Person(first:String, last:String) {
> def name(printer:(String,String): String) = printer(first,last)
> }
>
> object FunctionDemo {
> def main(args: Array[String]) {
> val me = new Person("Dave","Copeland")
>
> def casual(first:String,last:String) = first + " " + last
>
> println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
> println(me.name(casual))
> }
> }
>
> So, my question is why is the syntax this way? How can I mentally decompose
> the syntactical elements in a way that makes sense.
>
> Dave
>
--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@gmail.com
> class Person(first:String, last:String) {
> def name(printer:(String,String): String) = printer(first,last)
> }
How about: printer:String:String? Does this look.. consistent?
> println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
That looks to me like you are assigning f.toUpperCase + "_" +
l.toUpperCase to f and l, like this statement:
val (x, y) = (1, 2)
Scala's => is from the lambda syntax (I'll use Haskell syntax because
I don't have my Greek with me): \x -> x * 2
C# does it the same way, F# uses ->, the BGGA proposal for adding
lambdas to Java does it the same way. I'd rather not have to look
carefully to tell whether an = is part of a lambda or a side effect.
2009/7/21 David Copeland :
> I find that, on occasion, the syntax constructs in Scala surprise me, and I
> have a hard time deconstructing why they are the way they are.
>
> For example:
>
> class Person(first:String, last:String) {
> def name(printer:(String,String) => String) = printer(first,last)
> }
>
> object FunctionDemo {
> def main(args: Array[String]) {
> val me = new Person("Dave","Copeland")
>
> def casual(first:String,last:String) = first + " " + last
>
> println(me.name((f,l) => f.toUpperCase + "_" + l.toUpperCase))
> println(me.name(casual))
> }
> }
>
> In the first println, I create an anonymous function. What I find strange
> is the "=>". When I define a non-anonymous function/method (as in "def
> casual"), I use the equals sign (in fact, it is a syntax error to use the
> arrow). On its face, this seems like rather arbitrary syntax. Compound
> this with the fact that, when referring to a function type, you use the
> arrow to indicate return value (and not a colon, as in non-function types),
> and I feel like I'm just memorize arbitrary syntax rules that don't make
> much sense.
>
> As an example, the following feels more consistent to me:
>
> class Person(first:String, last:String) {
> def name(printer:(String,String): String) = printer(first,last)
> }
>
> object FunctionDemo {
> def main(args: Array[String]) {
> val me = new Person("Dave","Copeland")
>
> def casual(first:String,last:String) = first + " " + last
>
> println(me.name((f,l) = f.toUpperCase + "_" + l.toUpperCase))
> println(me.name(casual))
> }
> }
>
> So, my question is why is the syntax this way? How can I mentally decompose
> the syntactical elements in a way that makes sense.
>
> Dave
>