The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Page 189 - variable argument list. Common considerations about learning in

4 replies on 1 page. Most recent reply: Dec 21, 2010 10:23 PM by Jim Balter

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Page 189 - variable argument list. Common considerations about learning in Posted: Nov 2, 2008 3:25 PM
Reply to this message Reply
Advertisement
def echo(args: String*) = for (arg <- args) println(arg)

'... Thus, the type of args inside the echo function, which is declared as type "String*" is actually Array[String]'

I would mention in a footnote, that a syntax of passing variable length argument list in Scala closely resembles one in C/C++. I'm pretty sure Martin chose asterisk, because it is used in C/C++, although in C/C++ it denotes a pointer to a first element of array.
Another difference is that in C/C++ we would pass an array to such a function, not several arguments, separated by comma.
There is definitely a feel, that C++ is more functional language than Java, for example a name of a function is a pointer (reference) variable pointing to a function itself, which allows to pass a function to another function as parameter and return as return value. First-class functions, isn't it?
So, Scala feels partly as a consistent attempt to re-introduce to Java functional features which were dropped when Java creators used C++ to produce new [over?]simplified language.

Personally, I always loved when authors of books described new languages in comparison to existing ones, like Trey Nash is doing in his "Accelerated C# 2008", comparing C# to C++, like David Flanagan is doing in "JavaScript. The Definite Guide", comparing JavaScript to C/C++, and like Bruce Eckel is doing in "Thinking in Java", comparing it to... C++ again. (By the way, Bruce is considering Scala as a next "big" language, which will gradually and partially replace Java the same way Java did to C++.) One fresh and excellent example is "Real World Functional programming in .NET" by Tomas Petricek http://www.manning.com/petricek. This book introduces F# language (a merge of OCaml and .NET platform) and functional concepts in constant comparison to limited functional features of C# 3.0. It helps a lot to understand both F# and functional features of C#. It even helps to understand Scala, because functional concepts are the same in both F# and Scala.
To learn programming languages in comparison is not an easy task. But it is, in my opinion, a most powerful way to obtain real understanding of languages. Footnotes or special text blocks, describing similarities and diffrence between Scala and Java, Scala and Haskell, Scala and F# - makes a Scala book shines even more.


Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Re: Page 189 - variable argument list. Common considerations about learning Posted: Nov 2, 2008 8:30 PM
Reply to this message Reply
I've just posted a bit more philosophical thoughts about Scala approach vs. F# approach in introducing functional ideas to OOP world, about their simplicity/difficulty and chances for success. No, I don't feel like I'm a guru. I just feel it is extremely interesting and important to understand how each language fits into a common picture.

Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Re: Page 189 - variable argument list. Common considerations about learning Posted: Nov 9, 2008 3:56 PM
Reply to this message Reply
It'll be better to remove that spammer "rbter14 rbter14" account from this forum.

Andrew Phillips

Posts: 3
Nickname: demobox
Registered: Sep, 2009

Re: Page 189 - variable argument list. Common considerations about learning in Posted: Oct 28, 2009 2:22 AM
Reply to this message Reply
Not sure if this is the best place to post this, but it *is* about variable arguments ;-)

On p154 of version 5 (section 8.8), it is stated that "the type of args inside the echo function [def echo(args: String*) ...] is actually Array[String]".

According to the Scala language definition, should it not actually be Seq[String]? Cf. section 4.6.2 Repeated Parameters of the Scala Reference and

http://weblogs.java.net/blog/cayhorstmann/archive/2009/10/13/recursive-varargs-methods-scala

Jim Balter

Posts: 13
Nickname: jibal
Registered: Dec, 2010

Re: Page 189 - variable argument list. Common considerations about learning in Posted: Dec 21, 2010 10:23 PM
Reply to this message Reply
"I would mention in a footnote, that a syntax of passing variable length argument list in Scala closely resembles one in C/C++. I'm pretty sure Martin chose asterisk, because it is used in C/C++, although in C/C++ it denotes a pointer to a first element of array."

I would bet that it's because * is Kleene repetition (familiar from regular expressions) and that it has nothing to do with C or C++.

Flat View: This topic has 4 replies on 1 page
Topic: get rid of Rational's 'private val g'? Previous Topic   Next Topic Topic: Chapter 18 simulator

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use