Hacker News new | past | comments | ask | show | jobs | submit login

Scala is awesome, when combined with AKKA it's even better. We're much more efficient then we ever were in Java. Also, ops doesn't know it's not java since they can use all the same jvm tools.

val myList = "Jim" :: "Bob" :: Nil myList(println)

what's hard about that?




Heh to a Java programmer, the answer would be - everything.

Here are my thoughts on it. - I get val myList is some sort of list... - Seems to be a list of Strings. But what is ::? - Nil means list is nullable. Wait is one of the parts of list 'myList(println)' (looks a bit like Smalltalk)? - Why is println the argument and not the method? Is that syntax correct?!

Compare same thing in Fantom. Note ; is only used as a separator and is equal in all respects to '\n'.

     myList := ["Jim", "Bob", null]; echo(myList)


The :: is Scala's cons operator, the Nil means end-of-list. It's actually much more common in Scala to create a list like so:

  val nn = List("Jim","Bob")
This creates the same list that the example using :: did. It doesn't need to be explicitly terminated with Nil.


What does cons operator mean? Concatenation? Constructor? Other than hello world I haven't done much Scala.


Cons is short for construct and comes from Lisp which has weird names for things; things like car and cdr.

It means construct a new list.

A cons A (or A :: B) means create a new list with A before B, where B can be a list item or a list itself and A is a list item.

The :: operator is right associative so "Jim" :: "Bob" :: Nil actually works like ("Jim" :: ("Bob" :: Nil) ) (Actually all operators that end in : are right associative in Scala.)

For more see: http://en.wikipedia.org/wiki/Cons


I think you mean:

; println(myList)

i.e. reorder those and add a semicolon or put it on the next line

and I agree with pivo that "Jim" :: "Bob" :: Nil should be List("Jim", "Bob"). I'm not sure if these are the same as _Y_'s Fantom code, but both of these print out "List(Jim, Bob)" in Scala.


Apparently everything since that statement doesn't compile




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: