I'd like to do one for each of the three most-popular jvm languages, but since I know Scala best I'll stick to that for now:
Scala is a strong, statically-typed imperative/object oriented/functional language for the JVM. It was originally created by Martin Odersky, one of the creators of java's generics, as an experiment in fusing the object-oriented and functional programming paradigms. It has recently left the academic sphere and is now in use by the likes of Twitter, Foursquare, Sony, and others.
Why use Scala?
- Scala is fast! - Scala is on par with java in most benchmarks
- Scala has type inferencing - Scala is more strongly typed than Java, but its type inferencing means you only need to manually write types a fraction of the time.
- Scala is expressive - type classes, macros, for comprehensions, pattern matching, mixins and more
- Scala easily interfaces with java libraries - nearly all java libraries work in scala without any effort.
- Scala can be run on Android and other JVMs - Dalvik, OpenJDK, Oracle's JVM, and others can all run scala bytecode since scala compiles to standard jvm bytecode.
What are the downsides to Scala?
- Scala bytecode compatibility is brittle - Compared to Java, Scala bytecode is brittle. If you are using scala 2.10, you will have to use libraries compiled with scala 2.10. Right now brittleness only shows on major version changes (2.x changes).
- Scala is complex - Scala has many many many many features. So many in fact that I still learn new ones on a regular basis after using Scala for 2 years.
- Scala is as slow as C++ to compile - This is being worked on, and there are ways to work around the slow build times, but Scala takes around 2-3x as long to compile as Java.
How can I get Scala?
What IDEs are available?
Build Systems?
Where to start?
Example Code:
A quick hello world example in Scala:
Code:
object Main extends App {
println("hello world!!")
}
Classes compared to java:
Code:
//Scala
class Dog(name: String, legs: Int) { //Constructor
var homeAddress = "7 villa condorcet"
private var _age = 0
def run = println("the dog ran")
def age = _age //getter
def age_=(years: Int) { _age = years } //setter
}
//Java
class Dog {
public String homeAddress = "7 villa condorcet";
private int age = 0;
final public String name;
final public int legs;
public Dog(String name, int legs) {
this.name = name;
this.legs = legs;
}
void run() {
System.out.println("the dog ran");
}
int getAge() {
return age;
}
void setAge(int years) {
age = years;
}
}
A memoized fibonacci example:
Code:
object Memo extends App {
def memoize[A,B](fn: A => B) = {
val dict = new HashMap[A,B]
((x: A) => (dict get x) getOrElse {
val res = fn(x)
dict(x) = res
res
})
}
val fib: BigInt => BigInt = memoize ( _ match { case n if n < 2 => n; case n => fib(n-1) + fib(n-2) } )
fib(1000) // 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
}
Bonus - Scala Job Listings: http://www.scalajobz.com/
Bookmarks