DOC PREVIEW
Duke CPS 296.1 - A Scalable Language

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

4/15/20101A Scalable LanguageMartin Odersky, FOSDEM 2009Martin OderskyFOSDEM 20092The software landscape today …… resembles a tower of Babel with many little (or not so little) languages playing together.E.g.> JavaScript on the client>Perl/Python/Ruby/Groovyfor serverMartin Odersky, FOSDEM 2009Perl/Python/Ruby/Groovy for server side scripting> JavaFX for the UI> Java for the business logic> SQL for database accessall cobbled together with agenerous helping of XML.3This is both good and badGood: Every language can concentrate on what it’s best at.Bad: Cross language communication: complicated, fragile, source of misunderstandings.Martin Odersky, FOSDEM 2009Problematic: Cross language communication is controlled by a common type system (neither static nor dynamic).It's based on low-level representations such as XML trees or (worse) strings (as in JDBC database queries).4Alternative: Scalable languagesA language is scalable if it is suitable for very small as well as very large programs.A single language for extension scripts and the heavy lifting.Martin Odersky, FOSDEM 2009Application-specific needs are handled through libraries and embedded DSL's instead of external languages.Scala shows that this is possible.5Scala is a scripting languageIt has an interactive read-eval-print-loop (REPL).Types can be inferred.Boilerplate is scrapped.scala> var capital = Map("US" → "Washington", "France" → "Paris") Martin Odersky, FOSDEM 2009capital: Map[String, String] = Map(US → Washington, France → Paris) scala> capital += ("Japan" → "Tokio") scala> capital("France") res7: String = Paris6Scala is the Java of the futureIt has basically everything Java has now.(sometimes in different form) It has closures. (proposed for Java 7, but rejected) It has traits and pattern matching.(I would not be surprised to see them in Java 8, 9 or 10) It compiles to .class files, is completely interoperable and runs about as fast as JavaMartin Odersky, FOSDEM 2009Javaobject App { def main(args: Array[String]) { if (args exists (_.toLowerCase == "-help")) printUsage()elseprocess(args)}}4/15/201027InteroperabilityScala fits seamlessly into a Java environmentCan call Java methods, select Java fields, inherit Java classes, implement Java interfaces, etc.None of this requires glue code or interface descriptionsJava code can also easily call into Scala codeMartin Odersky, FOSDEM 2009yScala code resembling Java is translated into virtually the same bytecodes.⇒ Performance is usually on a par with Java8Scala is a composition languageNew approach to module systems:component = class or traitcomposition via mixinsAbstraction throughtrait Analyzer { this: Backend =>…}trait Backend extends AnalyzerithOtii tiMartin Odersky, FOSDEM 2009> parameters,> abstract members (both types and values),> self typesgives dependency injection for freewithOptimizationwith Generation {val global: Mainimport global._type OutputMedium <: Writable}9Is Scala a “kitchen-sink language”?Not at all. In terms of feature count, Scala is roughly comparable to today’s Java and smaller than C# or C++. But Scala is deep, where other languages are broad.Two principles: Martin Odersky, FOSDEM 20091. Focus on abstraction and composition, so that users can implement their own specialized features as needed.2. Have the same sort of constructs work for very small as well as very large programs.10Scala compared to JavaScala removesScala addsilt t t fit f+ i i iti ith t it- break, continue+ closures - primitive types+ operator overloading- static members+ a pure object systemMartin Odersky, FOSDEM 2009- raw types+ abstract types- wildcards+ existential types- enums+ pattern matching- special treatment of interfaces+ mixin composition with traitsModeled in libraries:assert, enums, properties, events, actors, using, queries, …11Scala cheat sheet (1): DefinitionsScala method definitions:def fun(x: Int): Int = {result}Java method definition:int fun(int x) { return result}Martin Odersky, FOSDEM 2009def fun = resultScala variable definitions:var x: Int = expressionval x: String = expression(no parameterless methods)Java variable definitions:int x = expressionfinal String x = expression12Scala cheat sheet (2): ExpressionsScala method calls:obj.meth(arg) obj meth argScala choice expressions:Java method call:obj.meth(arg) (no operator overloading) Java choice expressions, stmts: Martin Odersky, FOSDEM 2009if(cond) expr1 else expr2expr match {case pat1=> expr1....case patn=> exprn}cond ? expr1 : expr2 if (cond) return expr1; else return expr2;switch (expr) {case pat1: return expr1; ...case patn: return exprn ;} // statement only4/15/2010313Scala cheat sheet (3): Objects and ClassesScala Class and Objectclass Sample(x: Int, val p: Int) {def instMeth(y: Int) = x + y}Java Class with staticsclass Sample {private final int x;public final int p; Sample(int x, int p) { this.x = x; Martin Odersky, FOSDEM 2009object Sample {def staticMeth(x: Int, y: Int) = x * y}this.p = p;}int instMeth(int y) { return x + y; } static int staticMeth(int x, int y) {return x * y;}}14Scala cheat sheet (4): TraitsScala Traittrait T {def abstractMth(x: String): Intdef concreteMth(x: String) =+fi ldJava Interfaceinterface T {int abstractMth(String x)}Martin Odersky, FOSDEM 2009x + fieldvar field = “!”}Scala mixin composition:class C extends Super with T(no concrete methods) (no fields)Java extension + implementation:class C extends Super implements T15Spring CleaningScala’s syntax is lightweight and concise. Due to:> semicolon inference,> type inference,> lightweight classes,> extensible API’s,>lvar capital = Map("US" -> "Washington", "Canada" -> "ottawa") capital += ("Japan" -> "Tokyo") for (c <- capital.keys) capital(c) = capital(c) capitalizeMartin Odersky, FOSDEM 2009>closures ascontrol abstractions.Average reduction in LOC: ≥ 2due to concise syntax and better abstraction capabilitiesÎScala feels like a cleaned up Java …capital(c) = capital(c).capitalizeassert(capital("Canada") == "Ottawa") 16… with one major differenceIt's x: Int instead of int xWhy the change?Works better with type inference:varx = 0 instead ofx = 0 // that's not a definition!Martin Odersky, FOSDEM 2009Works better for large type expressions:val x: HashMap[String, (String, List[Char])] = …instead ofpublic final HashMap<String, Pair<String, List<Char>>> x = …17Scalability demands extensibilityTake numeric data typesToday's languages support int, long, float,


View Full Document

Duke CPS 296.1 - A Scalable Language

Documents in this Course
Lecture

Lecture

18 pages

Lecture

Lecture

6 pages

Lecture

Lecture

13 pages

Lecture

Lecture

5 pages

Load more
Download A Scalable Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A Scalable Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Scalable Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?