Archive for category Scala Experience

A little Scala Quiz ;-)

These two lines returns the same result. What is the name of the second line special shorthand ?

1) args.foreach( (arg : String) => println(arg))

2) args.foreach(println)

Response :  Partially applied function

It can be used when a function literal consist of one statement that takes a single argument.

No Comments

Scala Variables

Last week I read about Scala’s variable definition. I note an interesting way to define a variable which make me write this post.  Scala has two type of variables which can be defined with var and val. These key words shows how to use a variable. For example if we define variable with varvar name : String = “Some Name” ‘ that’s mean that we can change the value of the variable. Usage of val val name : String = “Some Name” ‘  is that we define constant value for some reason. If you try to assign a value to variable defined with val you will receive error “reassignment to val”.

What I like in this approach of defining variable is the fact that you  can show your desire to not change value for given variable very easy and you don’t need of modifiers which sometimes must be carried with the variable in some languages like C# and C++.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupery
No Comments

Hello World in Scala II ;-)

I did my brief research regarding Scala’s object concept. The notion behind the key word object is that every element in Scala language is object even the primitive type int.  So there isn’t even class level fields and methods. For example you can’t implement Singleton design pattern or to use static methods and fields. The way to represent static data or Singleton is definition of object. The Scala object is lazily instantiated by the system hence it’s doesn’t support constructor with multiple arguments. For the whole info regarding object please see http://programming-scala.labs.oreilly.com/ch07.html#ClassesAndObjects.

Tags:

No Comments

Hello World in Scala ;-)

Today I made my first experiment with Scala. In my humble opinion this is the new general purpose language for the next 5 years! Don’t ask me why! It’s my observation from the read posts and the fact that most gurus make their little experiments with Scala in the last 5-6 months for example Uncle Bob’s company and Michael Nygard and now here is my first example.

>> HelloWorld.scala

package hello

object HelloWorld extends Application {

println(”Hello, World!”)

}

My experience with Eclipse 3.5.1 and Scala plugin 2.7.7 final was that  Scala plugin had some problems to create for me HelloWorld Scala Object. It didn’t allow me to edit the HelloWorld.scala file. At the moment I didn’t understand the meaning of object in Scala but I will find out soon. I think that Scala language plugin isn’t so well prepared for production purposes see the posts in http://stackoverflow.com/ . We will see the future of Scala and whether I am right with my prediction! ;-)

Tags: ,

No Comments