why scala?

27
This talk is about how a serial language enthusiast settled down with a nice Swiss language called Scala.

Upload: alex-payne

Post on 08-Sep-2014

60.895 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Why Scala?

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

Scala.

Page 2: Why 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.

Page 3: Why Scala?

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.

Page 4: Why Scala?

I wanted: fastfunctionalexpressivestatically typedconcurrentbeautifula pony

My personal criteria for a good systems language.

Page 5: Why Scala?

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.

Page 6: Why Scala?

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.

Page 7: Why 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).

Page 8: Why Scala?

Features.

Page 9: Why Scala?

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.

Page 10: Why Scala?

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.

Page 11: Why Scala?

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.

Page 12: Why Scala?

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.

Page 13: Why Scala?

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.

Page 14: Why Scala?

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.

Page 15: Why Scala?

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.

Page 16: Why Scala?

More: case classespropertiesexistential typesimplicitslazy evaluationparser combinatorsmonadsannotations

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

Page 17: Why Scala?

Examples.

Page 18: Why Scala?

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.

Page 19: Why Scala?

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.

Page 20: Why Scala?

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.

Page 21: Why Scala?

Community.

Page 22: Why Scala?

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

Page 23: Why Scala?

Blogs &c. Code CommitCoderspielScala-BlogsGraceless FailuresDeliciousRedditFriendFeedTwitter Search

Page 24: Why Scala?

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

Page 25: Why Scala?

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

Page 26: Why Scala?

Irksome: Javalandsmall communitycomplexity

...but worth it.

The bad, the ugly.

Page 27: Why Scala?

Thank you. Questions?

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