<<

A scalable lan- guage works for very small and Scalable Programming very large pro- Languages Languages Today grams. A language is scalable if it is suitable Scripting Script writers are Cross-languages for very small as well as very large primarily concerned with con- The Tower of Babel’s construction programs. This means that a single ciseness. Scala’s type inference, stopped when its builder started speak- communication language can be used both for exten- efficient syntax and boiler- becomes unnec- sion scripts and for the heavy lifting. plate-scrapping features all fit ing too many different languages. Are Domain-specific needs are provided this requirement perfectly. An essary. for by libraries and embedded lan- interactive shell is available. we seeing the same effect in software? guages, instead of external tools. If a domain-specific concern is Scala shows that such languages can Composition There is no dedi- JavaScript on the client, Python for server-side scripting, for busi- embedded in a program, it is exist. It is equally suitable for finan- cated module system in Scala. ness logic and SQL for database access, all cobbled together with XML. possible to type-check it w.r.t. cial applications, massive multiplayer Instead, classes and traits can This is quite typical for large software systems today. the rest of the program. Scala online games, web frameworks such be composed via mixins. Mod- Each language may be used at what it does best. But cross-language embeds concerns such as rela- as Lift or compilers. Unsurprisingly, ule abstraction is obtained communication must rely on a “lowest common denominator” like XML tional queries or grammars as the Scala compiler itself is written in through type parameters, type or worse, strings (as in SQL). This complicates deployment, makes sys- libraries in a type-safe way. Scala. members and self types. tems fragile and is a big source of misunderstandings and errors.

Research Today, Scala is a language that scales down and up eas- Martin Odersky with the current Except for type annotations, ily. It also works well with its mixed community of expert (design- Scala’s syntax is very similar to ing the framework) and nonexpert users. The original goal of de- and past LAMP team: Philippe Al- Java’s. On the other hand, fea- tures such as semicolons and signing a more expressive language is all but reached: small and therr, Vincent Cremet, Iulian Dra- large problems can be solved, experts and beginners can use the Scala type inference and lightweight language at their own level. gos, Gilles Dubochet, Burak Emir, classes and functions mean However, Scala’s capability to guarantee safety is still limited, de- vs. Java Scala’s syntax feels a lot lighter. spite its rich type system. Domain-specific safety properties cannot Sebastian Hack, Philipp Haller, always be encoded in the type system. Various research projects Sean McDirmid, Ingo Maier, at LAMP are looking at means to provide pluggable type-systems In Scala In Java and type annotations. Adriaan Moors, Stéphane Mich- Method definitions Also, our claim that “everything can be implemented as a library” eloud, Nikolay Mihaylov, Lukas still remains contentious, and LAMP is researching means to im- def mth(x: Int): Int = { int mth(int x) { result } return result; } plement modern language features, such as transactions or em- Rytz, Michel Schinz, Alexander bedded query languages, as libraries. Variable definitions Spoon, Erik Stenman, Geoffrey var x: Int = … int x = …; Washburn and Matthias Zenger. val s: String = … final String s = …; Method calls obj.mth(arg) obj.mth(arg); obj mth arg no operator overloading

This poster was conceived by Gilles Dubochet Choice expressions if (cond) exp1 else exp2 cond ? exp1 : exp2; scala expr match { switch (expr) { case pat1 => exp1 case pat1 : return exp1; case patn => expn case patn : return expn; } } Every value in Scala is an object. Scala is a functional language Java’s deviations from a pure in the sense that every func- Classes object model — primitives, tion is a value. Functions can be class Sample (x: Int, class Sample { statics — have been removed. Object- anonymous, curried or nested. val p: Int) private final int x; Functions are objects too: their Many useful higher-order func- { public final int p; def mth1(y: Int) = … Sample(int x, int p) { behaviour is implemented by tions are implemented as meth- } this.x = x; their “apply” method. They can Oriented Functional ods of Scala classes. this.p = p; } be specialised by extension. A function can be partial if it is int mth1(int y) { Scala’s object model is richer abstract class IntQ { not defined on all of its domain. return …; } than that of Java, and permits def get: Int Many algorithms It can be tested for whether it is } a form of multiple inheritance. def put(x: Int) defined on a given value. Objects } are written con- Besides parametric class types, Pattern matching blocks are object Sample { class Sample { class ImpQ extends IntQ { similar to those of Java, Scala partial functions, which allows def mth2(x: Int) = … static int mth2(int x) { private val buf = cisely using func- provides structural, existential complex control structures to } return …; } new ArrayBuffer[Int] tional idioms. } and path-dependant types. def get = buf.remove(0) be expressed easily. def put = buf += x Traits and mixins } Example A queue of integers Example The quick-sort al- def sort(list: List) = trait T { interface T { trait Dbl extends IntQ { is defined and implemented gorithm is implemented on list match { def mth1(x: String): Int int mth1(String x); abstract override (ImpQ). Dbl modifies any queue’s the right. The first element be- case Nil => Nil def mth2(x: Int) = … } def put(x: Int) = case pvt :: rst => var field = … behaviour. A new instance q comes the pivot (pvt) and quick no concrete methods or fields super.put(2*x) sort(rst filter (_ < pvt)) } is created, inheriting both the } sort is recursively called on all ::: List(pvt) ::: class C extends Sup with T class C extends Sup standard implementation and val q = new ImpQ with DblQ elements smaller, respectively sort(rst filter (_ > pvt)) implements T the doubling behaviour. q.put(1); q.get == 2 larger than pvt. }

Scala fits seamlessly into a Java Example The ping-pong program is a val player1 = actor { environment. All of Java’s con- fascinating concurrent system where loop { react { cepts map transparently to two players send each other “ping”, case Ping => sender ! Pong Scala. It is possible to call Java respectively “pong” messages until Inter- Concurrency using case Stop => exit methods, select fields, inherit they get bored. } } classes or implement interfac- Actors are defined using the actor } operable es from Scala code. Actors and Messages method. The react loop of the actor val player2 = actor { Java frameworks and tools, like is defined as a partial function on player1 ! Ping loop { react { jdb, Wicket, , Spring or Actors implement the concurrent all messages it can receive at that case Pong => Seen at the byte- Terracotta also just work. Actors encapsu- programming model of Erlang in Sca- point. if (gotBored) { Performance is comparable la. Like objects, actors have state and An actor will block until one message sender ! Stop code level, Scala late state and be- exit with Java, and JVM improve- behaviour. Unlike objects, they do on which it reacts is received. ments benefit directly to Scala. } is just another haviour (like ob- not communicate through method An actor sends messages using the else sender ! Ping Scala libraries can also be used calls but by sending asynchronous send (!) operator and the implicit } } Java library. from Java, albeit not as easily. jects). They are messages to other actors’ mailboxes. sender reference. } also active and The treatment of incoming messages Example The Scala program on val s: ServerSocketChannel = … is done on an actor’s thread so that all Performance It is not necessary to Inversion of control In pub- the right implements a minimal while (s.isOpen) { communicate actors work concurrently. If an actor’s map each actor to a JVM thread. In- lish-subscribe, a reaction to a echo server using Java’s NIO li- val client = s.accept mailbox is empty, the actor blocks stead, a thread pool is shared amongst message is in the publisher de- val bs = brary. Java classes and meth- through asyn- until it receives something. actors so that blocked actors do not spite it conceptually being part ByteBuffer.allocate(4096) ods, like ByteBuffer of write, client.read(bs) chronous mes- Concurrent actors can synchronize use scarce resources. Performance of of the subscriber’s behaviour. are used transparently and have bs.flip by waiting for messages. This is safer actor-based concurrent application In contrast, control in actors is no performance overhead. client.write(bs) } sage passing. than lock-based synchronization. was found to be very high. not inverted.