In 2009, when Scala was just getting popular, I tried to build a data-pipeline system based on Scala. At the time, the intention was to build an ETL system where data flow is defined as spring configuration files. The project went very well for a few weeks and then I moved on to better (?) things. But at the time, I really liked Scala.
Later, a month or two ago, I tried my hand at Scala only to dislike it. There is a lot of functionality baked into the Language - implicits, fuction blocks, convention methods such as apply(), etc. I did not enjoy looking at it & add to that there has been “Scala Sucks” articles that I came across. At this time, I did not really like Scala
For the past week or so, I have been closely following Functional Programming in Scala on Coursera (well, not really doing the assignments but may be I will start very soon). That renewed my like-ness for Scala. I am also following the Principles of Reactive Programming and am into the lectures given by Erik Meijer - which by the way are totally awesome. Erik is a better teacher than Martin :). Anyway, now I am beginning to like Scala (frankly, I am forcing myself to get used to it enough to like it again).
The intention here is to list a bunch of things that I know about scala that are different from regular language such as C#. This is to see how much of the content has made through into my head.
For example, if you have a function which returns another function of type “Int => Boolean” as shown below.
You can change it to:
Effective Scala entry on “type alias”. And type alias is also used in something called abstract types. For a stackoverflow discussion (lengthy-one) on abstract types vs generics
Traits in scala are what most people in the community say “what interfaces should have been”. Traits, like abstract classes, allows you to define default implementation for methods. But traits allows you to “mixin”.
In the example below (borrowed), shows an example which uses - abstract type usage, traits, mixin class composition with traits.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Interface
trait AbsIterator {
type T //abstract type
def hasNext: Boolean
def next: T
}
class StringIterator(s: String) extends AbsIterator {
type T = Char
private var i = 0
def hasNext = i < s.length()
def next = { val ch = s charAt i; i += 1; ch }
}
trait RichIterator extends AbsIterator {
def foreach(f: T => Unit) { while (hasNext) f(next) }
}
object Main extends App {
//mixin
var strIter = new StringIterator("Krishna") with RichIterator
strIter foreach println
var strIter2 = new StringIterator("Vangapandu")
//invalid declaration below.
//var foreached = strIter2 with RichIterator[Char]
//create a class that does the mixins
class StringWithForEach(str: String)
extends StringIterator(str)
with RichIterator
//any instance will have foreach
var foreached = new StringWithForEach("Vangapandu")
foreached foreach println
}