The Scala Language Specification
Total Page:16
File Type:pdf, Size:1020Kb
The Scala Language Specification Version 1.0 DRAFT November 16, 2005 Martin Odersky Philippe Altherr Vincent Cremet Burak Emir Stéphane Micheloud Nikolay Mihaylov Michel Schinz Erik Stenman Matthias Zenger PROGRAMMING METHODS LABORATORY EPFL SWITZERLAND Contents I Rationale 1 II The Scala Language Specification 7 1 Lexical Syntax 9 1.1 Identifiers .................................... 10 1.2 Braces and Semicolons ............................. 11 1.3 Literals ...................................... 11 1.4 Whitespace and Comments .......................... 11 1.5 XML mode .................................... 12 2 Identifiers, Names and Scopes 13 3 Types 15 3.1 Paths ....................................... 16 3.2 Value Types ................................... 16 3.2.1 Singleton Types ............................. 16 3.2.2 Type Projection ............................. 16 3.2.3 Type Designators ............................ 17 3.2.4 Parameterized Types .......................... 17 3.2.5 Compound Types ............................ 18 3.2.6 Function Types ............................. 18 3.3 Non-Value Types ................................ 19 3.3.1 Method Types .............................. 19 3.3.2 Polymorphic Method Types ...................... 19 3.4 Base Classes and Member Definitions .................... 20 3.5 Relations between types ............................ 22 iv CONTENTS 3.5.1 Type Equivalence ............................ 22 3.5.2 Conformance .............................. 22 3.6 Type Erasure ................................... 24 3.7 Implicit Conversions .............................. 24 4 Basic Declarations and Definitions 27 4.1 Value Declarations and Definitions ..................... 28 4.2 Variable Declarations and Definitions .................... 29 4.3 Type Declarations and Type Aliases ..................... 30 4.4 Type Parameters ................................. 32 4.5 Function Declarations and Definitions ................... 34 4.6 Overloaded Definitions ............................ 36 4.7 Import Clauses ................................. 36 5 Classes and Objects 39 5.1 Templates .................................... 39 5.1.1 Constructor Invocations ........................ 40 5.1.2 Base Classes ............................... 40 5.1.3 Evaluation ................................ 41 5.1.4 Template Members ........................... 42 5.1.5 Overriding ................................ 42 5.1.6 Modifiers ................................. 43 5.1.7 Attributes ................................ 45 5.2 Class Definitions ................................ 46 5.2.1 Constructor Definitions ........................ 47 5.2.2 Case Classes ............................... 48 5.3 Traits ....................................... 50 5.4 Object Definitions ............................... 51 6 Expressions 53 6.1 Literals ...................................... 54 6.2 Designators ................................... 55 6.3 This and Super ................................. 55 6.4 Function Applications ............................. 57 CONTENTS v 6.5 Type Applications ................................ 58 6.6 References to Overloaded Bindings ..................... 58 6.7 Instance Creation Expressions ........................ 60 6.8 Blocks ....................................... 60 6.9 Prefix, Infix, and Postfix Operations ..................... 61 6.10 Typed Expressions ............................... 62 6.11 Method closures ................................. 63 6.12 Assignments ................................... 63 6.13 Conditional Expressions ............................ 65 6.14 While Loop Expressions ............................ 65 6.15 Do Loop Expressions .............................. 66 6.16 Comprehensions ................................ 66 6.17 Return Expressions ............................... 68 6.18 Throw Expressions ............................... 68 6.19 Try Expressions ................................. 69 6.20 Anonymous Functions ............................. 69 6.21 Statements .................................... 70 7 Pattern Matching 73 7.1 Patterns ...................................... 73 7.1.1 Regular Pattern Matching ....................... 74 7.2 Pattern Matching Expressions ......................... 77 8 Views 79 8.1 View Definition ................................. 79 8.2 View Application ................................ 79 8.3 Finding Views .................................. 80 8.4 View-Bounds .................................. 81 8.5 Conditional Views ............................... 84 9 Top-Level Definitions 85 9.1 Packagings .................................... 85 10 Local Type Inference 87 vi CONTENTS 11 XML expressions and patterns 89 11.1 XML expressions ................................ 89 11.2 XML patterns .................................. 91 12 The Scala Standard Library 93 12.1 Root Classes ................................... 93 12.2 Value Classes ................................... 95 12.2.1 Class Double .............................. 95 12.2.2 Class Float ............................... 95 12.2.3 Class Long ................................ 96 12.2.4 Class Int ................................ 96 12.2.5 Class Short ............................... 97 12.2.6 Class Char ................................ 97 12.2.7 Class Short ............................... 97 12.2.8 Class Boolean ............................. 98 12.2.9 Class Unit ................................ 98 12.3 Standard Reference Classes .......................... 98 12.3.1 Class String .............................. 98 12.3.2 The Tuple classes .......................... 99 12.3.3 The Function Classes ....................... 99 12.3.4 Class Array ............................... 99 12.4 The Predef Object .............................. 100 12.5 Class Node .................................... 101 A Scala Syntax Summary 105 B Implementation Status 111 IRATIONALE 3 There are hundreds of programming languages in active use, and many more are being designed each year. It is therefore hard to justify the development of yet an- other language. Nevertheless, this is what we attempt to do here. Our argument is based on two claims: Claim 1: The raise in importance of web services and other distributed soft- ware is a fundamental paradigm shift in programming. It is comparable in scale to the shift 20 years ago from character-oriented to graphical user inter- faces. Claim 2: That paradigm shift will provide demand for new programming lan- guages, just as graphical user interfaces promoted the adoption of object- oriented languages. For the last 20 years, the most common programming model was object-oriented: System components are objects, and computation is done by method calls. Meth- ods themselves take object references as parameters. Remote method calls let one extend this programming model to distributed systems. The problem of this model is that it does not scale up very well to wide-scale networks where messages can be delayed and components may fail. Web services address the message delay prob- lem by increasing granularity, using method calls with larger, structured arguments, such as XML trees. They address the failure problem by using transparent replica- tion and avoiding server state. Conceptually, they are tree transformers that con- sume incoming message documents and produce outgoing ones. Why should this have an effect on programming languages? There are at least two reasons: First, today’s object-oriented languages are not very good at analyzing and transforming XML trees. Because such trees usually contain data but no methods, they have to be decomposed and constructed from the “outside”, that is from code which is external to the tree definition itself. In an object-oriented language, the ways of doing so are limited. The most common solution [W3Ca] is to represent trees in a generic way, where all tree nodes are values of a common type. This makes it easy to write generic traversal functions, but forces applications to operate on a very low conceptual level, which often loses important semantic distinctions present in the XML data. More semantic precision is obtained if different internal types model different kinds of nodes. But then tree decompositions require the use of run-time type tests and type casts to adapt the treatment to the kind of node en- countered. Such type tests and type casts are generally not considered good object- oriented style. They are rarely efficient, nor easy to use. By contrast, tree transformation is the natural domain of functional languages. Their algebraic data types, pattern matching and higher-order functions make these languages ideal for the task. It’s no wonder, then, that specialized languages for transforming XML data such as XSLT are functional. Another reason why functional language constructs are attractive for web-services is that mutable state is problematic in this setting. Components with mutable state 4 are harder to replicate or to restore after a failure. Data with mutable state is harder to cache than immutable data. Functional language constructs make it relatively easy to construct components without mutable state. Many web services are constructed by combining different languages. For instance, a service might use XSLT to handle document transformation, XQuery for database access, and Java for the “business logic”. The downside of this approach is that the necessary amount of cross-language glue can make applications cumbersome to write, verify, and maintain. A particular problem is that cross-language interfaces