logo       

Re: assorted beginner questions: msg#00459

lang.scala

Subject: Re: assorted beginner questions

Yang,

Your questions make very good additions to the Scala FAQ. Thanks for asking them.


On 1/28/07, Yang < 6gcgy2n02@xxxxxxxxxxxxxx> wrote:
i've accumulated a list of questions over the past few days of using
scala. i always first try google then irc (i guess i scared everyone
away), but apologies if i've missed something obvious. they're mostly
superficial/easy to answer questions, hopefully... :) (perhaps less so
toward the end)

i get "value mkString is not a member of
scala.collection.mutable.Set[Obj]" but according to the API,
scala.Iterable defines mkString. how can i use this and other Iterable
methods (forall, etc.) on things like Sets?

Which version did you use? mkString is in Iterable only since 2.3.2.

for the following code:
    def all(xs: Iterator[Boolean]) = xs forall (x => x) // forall (xs) (x => x)
    all(for (val b <- List(true,false)) yield !b)
i get:
Main.scala:29: error: no type parameters for method map:
((scala.Boolean) => b)scala.List[b] exist so that it can be applied to
arguments ((scala.Boolean) => scala.Boolean)
--- because ---
result type scala.List[b] is incompatible with expected type
scala.Iterator [scala.Boolean]
    all(for (val b <- List(true,false)) yield !b)

Lists are not iterators. You either need to  turn the result of `for' into an iterator using `elements', or define `all' on Iterables.

are there any form of nested comments in scala?

Yes. /* ... */ comments nest.

does scala have keyword arguments a la lisp/python?

No. You can use anonymous classes and inheritance to work around this. David gave an example.

is there any special syntax for hashtable/map/set gets/updates?

General syntactic sugaring applies. You can write:

    m(key) = value       or
    m += (key -> value)

The two are equivalent. The first is translated to m.update(key, value). The second reduces to the same _expression_.

can i define functions that aren't part of an 'object' or 'class'?

Yes, as long as you execute them as a script in the interpreter shell.

'static methods'?

These go into an object. Example:

   object HashSet {
      def empty[T]: Set[T]
   }

   class HashSet[T] { ... }

HashSet.empty would be static in Java. In Scala, the object and the class HashSet are combined in one classfile. The object effectively contains the static parts.

is there a web interface to sbaz somewhere?

I think Ross Judson did a GUI interface. I am not sure whether it works over the web.

what's the keyword to look for for info about varargs? what is this
called in scala? (def xxx(y: int*) = ...) can defs be overloaded?

It's called a repeated parameter.

is there a more succinct way to reduce a list, e.g. something that
simply names an operator, like (mylist reduceLeft &&)?

For methods like && you need to give a lambda abstraction, i.e.

  mylist reduceLeft ((x, y) => x && y)

in pattern matching, can i bind to the composite structure as well as
its elements? in haskell you can do this as:
case blah of
  ls@(x:xs) -> ... -- i can refer to the cons (ls) or its car (x) and cdr (xs)
  [] -> ...

Yes, it's almost the same in Scala:

  blah match {
    case ls @ (x :: xs) => ...
    case List() => ...
  }


when can/can't i omit () (for zero-param calls/defs)

() in a call can be omitted for defs with () parameters. It does not work the other way round. I.e. if the def is parameterless, the call may not have a () argument.

or the 'new'
keyword preceding constructor calls?

If the class is a case class.

for the former, is it ever
ambiguous whether we're passing around a function or the function's
result? for the latter, is there a semantic difference?

No and no.

why is, e.g., foldLeft defined so oddly? 'explicitly curried', it seems:
def foldLeft[b](z: b)(op: (b, a) => b): b = ...
this is also seen on
http://www.scala-lang.org/intro/targettyping.html. is this seems to
encourage the user to use this strange syntactic style:
(xs foldLeft z) { (x,y) => x+y }

That's indeed the encouraged style. One reason for this is that then you need not give types for the parameters x and y (they are inferred from the previous argument).

while on that page, what's the difference between {} and () in this
context? it seems whileLoop treats both as parameterless closures
('blocks' as the manual calls them).

They are exactly the same at the moment. Both mean the unit value.  We are thinking of deprecating () because it looks too much like an empty parameter list (which is not the same as the unit value).

what's the 'intended' way to use forall? ideally, the syntax would be:
    assert(forall ({name, Prop(typ, value) <- props.elements) {
      checkType(value, typ)
    })
the closest thing that i believe is realizable is:
    assert(forall (props.elements) {
      {name, Prop(typ, value)} =>
        checkType(value, typ)
    })
but that gives me "error: malformed formal parameter list."

Try:
 
assert(forall (props.elements) {
      case {name, Prop(typ, value)} =>
        checkType(value, typ)
    })

If your closure contains a pattern, it needs to be preceded by case.

is there any way to automatically inherit certain behaviors, a la
'derives' in haskell? any tools/approaches using metaprogramming,
reflection, or something else? in particular, i'd like to
(de)serialize my objects, a la python's pickle or haskell's read/show,
without writing any serialization code (or at most once).

There are some frameworks for this in development. Philip Haller has a library of pickling combinators in style of what Andrew Kennedy did for Haskell.

what are the some of the principal reasons for scala's (generally)
lesser performance compared with java, at least according to the
language shootout? not trolling; i'd just like to understand the
strengths and weaknesses of languages that i use.

Last I saw, Scala was equal to Java in the shootout. I think the shootout produces a lot of noise anyway.

are there any efforts under way to provide static
checking/modification tools for scala? (checkers in the vein of
pmd/findbugs, or annotated like esc/dbc, and modifiers like
refactorers or transformers like jackpot)

I would like to encourage any such effort! I think this would be really useful tools, which could be more more convenient to use, given Scala's more advanced syntactic and typing capabilities.

Cheers

 -- Martin

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise