why scala?

Post on 08-Sep-2014

60.896 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Why Scala? How a serial language enthusiast settled down with a nice Swiss language.

TRANSCRIPT

This talk is abouthow a serial language enthusiast settled down with a nice Swiss language called

Scala.

Hi, I’m al3x, and I’m an addict a serial language enthusiast.

Came to previous C4s for Io, Erlang. Have programmed for money in Java, JavaScript, C, PHP, Perl, Ruby, and Python. Have programmed for fun, love, or spite in x86 Assembly, Erlang, Lua, Objective-C, various Lisps, Haskell, O-Caml, C++, Io, Erlang.

I work at Twitter.Twitter has used mostly Ruby.Ruby is good for some things.But not everything.

Ruby’s poor VM performance, monkeypatching and cultural issues, questionable for large systems. We had new services to build, didn’t want to continue with Ruby.

I wanted: fastfunctionalexpressivestatically typedconcurrentbeautifula pony

My personal criteria for a good systems language.

Our language shortlist: C

C++PythonErlang

Java[JVM dynamic languages]

Why not these languages? Talk about their issues. We ultimately knew we wanted a JVM language, just not Java.

Ops doesn’t know it’s not Java.

The JVM has chops. Address Java interoperability, Martin Odersky’s credentials, etc. Worse comes to worse, just fall back to Java. Most other JVM-hosted languages can’t claim the same speed as Scala.

Once we were informed, we went for Scala.

Bringing Martin Odersky into the office, Scala lift-off inspired confidence. Side projects helped (see, side projects are awesome).

Features.

Type inferencethat does not suck.

Static typing is a blessing and a curse. Scala lets you ignore typing until you can put it to work for you.

Immutable,save where mutability fits.

A pragmatic approach to data structure mutability. Most data structures available in mutable and immutable flavors. Requires thoughtful decision-making, but that pays off.

First-class functions,because we are classy people.

Once you have this feature in a language, you simply can’t do without it. Passing functions around is awesome - a bit like function pointers. Also: “higher-order” methods (methods that take other methods, like ‘map’), anonymous methods. Everything is an expression, everything has a value.

Traits, because inheritance isn’talways what you want.

Once you’ve gotten used to Ruby’s modules, it’s another difficult language feature to give up. Subclassing can be a nightmare, and isn’t always the correct design solution. Traits enable powerful abstraction of cross-cutting concerns.

Pattern matching:case statements on crack.

Every programmer matches patterns all day, every day. It’s just that most languages don’t have a lexical facility for making that task easier.

XML literals and query methods make working with XML positively tolerable.

XML ain’t going away any time soon. If you have to interact with XML a lot, Scala is an awesome choice. It’s got that JavaScript+JSON peanut butter n’ jelly quality.

Actors: a nice way to handle concurrency (but not the only way).

The “concurrency crisis” has been underway since the early 1990s. Ref: HOPL2 intro speech.Scala handles actors as nicely as Erlang and Io. Actors are neat, but we haven’t made extensive use of them at Twitter just yet. Also Pi and Join Calculi.

More: case classespropertiesexistential typesimplicitslazy evaluationparser combinatorsmonadsannotations

Address those features particular to Scala: case classes, properties, existential types, implicits.

Examples.

object HelloEvent { def main(args: Array[String]) { val event = { if (args.isEmpty) { "C4[2]" } else { args(0) } }

println("Hello " + event) }}

/* outputs:$ scala HelloEventHello C4[2]

$ scala HelloEvent "clown college graduates"Hello clown college graduates*/

Illustrates use of functions, Ruby-like convienence method on array, type system, values, singleton syntax.

package com.twitter.api_test

import com.twitter.api_test.formats._import org.apache.commons.httpclient._

trait RestResource { // much code snipped

def withFormats(formats: List[String])(fun: String => TwitterFormat): List[TwitterFormat] = { formats.map(format => fun(format)) }

def withAllFormats(fun: String => TwitterFormat): List[TwitterFormat] = { withFormats(List("xml", "json", "rss", "atom"))(fun) }}

Illustrates packages, imports, traits, currying, more type system stuff.

package com.twitter.api_test.test

import com.twitter.api_test._import org.specs._

object StatusesSpec extends Specification { var status: Status = null

"/statuses/public_timeline" should { doBefore { status = new Status() }

"validate all formats" in { val responses = status.withAllFormats { format => status.publicTimeline(format) } responses must notBeEmpty } }}

Illustrates Specs, vars, the ability of Scala to create DSLs.

Community.

Education: Programming in ScalaThe Busy Java Developer’s Guide...Why Scala?Introduction to High-Level Prog’ing...Scala Language Specification

Blogs &c. Code CommitCoderspielScala-BlogsGraceless FailuresDeliciousRedditFriendFeedTwitter Search

Tools: sudo port install scalaTextMateEmacs, VIMIDEs: Eclipse, NetBeans, IntelliJXcodeJavaRebel

Libraries and frameworks LiftSpecsScalaCheckConfiggyScalax, Scalazevery Java library evereven libraries in other JVM languages!

Irksome: Javalandsmall communitycomplexity

...but worth it.

The bad, the ugly.

Thank you. Questions?

Typeset in Avenir and Deja Vu Sans MonoDesigned by al3x in California

top related