YouTip LogoYouTip

Scala Tutorial

EPFL Programming Methods Laboratory # Scala Language Specification Version: 2.7 Original Author: Martin Odersky Translators: Gao De, Zhao Wei 2010-7-20 Table of Contents 1. Lexical ..................................................... 1 1.1. Identifiers ............................................. 1 1.2. Newline Characters ............................................ 2 1.3. Literals ............................................. 5 1.3.1. Integer Literals ................................... 5 1.3.2. Floating Point Literals .................................. 5 1.3.3. Boolean Literals .................................. 6 1.3.4. Character Literals .................................. 6 1.3.5. String Literals .................................. 6 1.3.6. Escape Sequences ..................................... 7 1.3.7. Symbol Literals ................................... 8 1.4. Whitespace and Comments .......................................... 8 1.5. XML Mode ........................................... 8 2. Identifiers, Names and Scopes ........................................... 11 3. Types .................................................... 13 3.1. Paths .............................................. 14 3.2. Value Types ............................................ 14 3.2.1. Singleton Types .................................... 14 3.2.2. Type Projection .................................... 14 3.2.3. Type Designators .................................... 15 3.2.4. Parameterized Types .................................. 15 3.2.5. Tuple Types .................................... 16 3.2.6. Annotated Types .................................... 16 3.2.7. Compound Types .................................... 16 3.2.8. Infix Types .................................... 17 3.2.9. Function Types .................................... 18 3.2.10. Existential Types .................................... 18 3.2.11. Primitive Types Defined in Predef ....................... 20 3.3. Non-Value Types ........................................... 20 3.3.1. Method Types .................................... 20 3.3.2. Polymorphic Method Types ................................ . 21 3.3.3. Type Constructors .................................. 21 3.4. Basic Types and Member Definitions .................................. 22 3.5. Relations between Types ....................................... 23 3.5.1. Type Identity .................................... 23 3.5.2. Conformance ...................................... 23 3.6. Volatile Types ........................................... 25 3.7. Type Erasure ........................................... 25 4. Basic Declarations and Definitions ............................................ 28 4.1. Value Declarations and Definitions ....................................... 28 4.2. Variable Declarations and Definitions ...................................... 29 4.3. Type Declarations and Type Aliases .................................. 31 4.4. Type Parameters ........................................... 32 4.5. Variance Annotations ........................................... 33 4.6. Function Declarations and Definitions ...................................... 34 4.6.1. Call-by-Name Parameters .................................... 35 4.6.2. Repeated Parameters .................................... 35 4.6.3. Procedures ....................................... 36 4.6.4. Method Return Type Inference ............................. 37 4.7. Import Clauses ....................................... 37 5. Classes and Objects ................................................. 39 5.1. Templates .............................................. 39 5.1.1. Constructor Invocations .................................. 40 5.1.2. Class Linearization .................................. 41 5.1.3. Class Members ...................................... 42 5.1.4. Overriding ....................................... 42 5.1.5. Inheritance Closures .................................... 43 5.1.6. Early Definitions .................................... 43 5.2. Modifiers ............................................ 44 5.3. Class Definitions ............................................ 46 5.3.1. Constructor Definitions .................................. 48 5.3.2. Case Classes ..................................... 49 5.3.3. Traits ....................................... 50 5.4. Object Definitions ........................................... 51 6. Expressions ................................................... 53 6.1. Expression Typing ....................................... 54 6.2. Literals ............................................ 54 6.3. Null Values ........................................... 54 6.4. Designators ............................................ 55 6.5. This and Super ...................................... 55 6.6. Function Applications ........................................... 56 6.7. Method Values ............................................ 57 6.8. Type Applications ........................................... 58 6.9. Tuples .............................................. 58 6.10. Instance Creation Expressions .................................. 58 6.11. Blocks ......................................... 59 6.12. Prefix, Infix and Postfix Operations ............................. 60 6.12.1. Prefix Operations .................................... 60 6.12.2. Postfix Operations .................................... 60 6.12.3. Infix Operations .................................... 60 6.12.4. Assignment Operators .................................... 61 6.13. Typed Expressions .................................. 61 6.14. Annotated Expressions ...................................... 62 6.15. Assignments ........................................... 62 6.16. Conditional Expressions ...................................... 63 6.17. While Loop Expressions ................................ 63 6.18. Do Loop Expressions ................................... 64 6.19. For Comprehensions ...................................... 64 6.20. Return Expressions ................................... 66 6.21. Throw Expressions .................................... 66 6.22. Try Expressions ...................................... 66 6.23. Anonymous Functions ....................................... 67 6.24. Statements ........................................... 68 6.25. Implicit Conversions ....................................... 69 6.25.1. Value Conversions ...................................... 69 6.25.2. Method Conversions .................................... 69 6.25.3. Overloading Resolution .................................... 69 6.25.4. Local Type Inference ................................ . 71 6.25.5. Eta Expansion .................................... 73 7. Implicit Parameters and Views ............................................ 75 7.1. The implicit Modifier .................................... 75 7.2. Implicit Parameters ........................................... 75 7.3. Views .............................................. 78 7.4. View Bounds ........................................... 79 8. Pattern Matching ................................................. 81 8.1. Patterns .............................................. 81 8.1.1. Variable Patterns .................................... 82 8.1.2. Typed Patterns .................................. 82 8.1.3. Literal Patterns .................................. 82 8.1.4. Stable Identifier Patterns ............................... 82 8.1.5. Constructor Patterns .................................. 83 8.1.6. Tuple Patterns .................................... 83 8.1.7. Extractor Patterns .................................... 83 8.1.8. Pattern Sequences .................................... 84 8.1.9. Infix Operator Patterns ............................... 84 8.1.10. Pattern Alternatives .................................... 84 8.1.11. XML Patterns .................................... 84 8.1.12. Regular Expression Patterns ............................... 84 8.1.13. Irrefutable Patterns .................................... 85 8.2. Type Patterns ........................................... 85 8.3. Type Parameter Inference in Patterns ................................ . 85 8.4. Pattern Matching Expressions ...................................... 87 8.5. Pattern Matching Anonymous Functions ................................ .... 88 9. Top-Level Definitions ................................................. 91 9.1. Compilation Units ........................................... 91 9.2. Packages .............................................. 91 9.3. Package References ............................................ 91 9.4. Programs .............................................. 92 10. XML Expressions and Patterns ....................................... 93 10.1. XML Expressions ...................................... 93 10.2. XML Patterns ....................................... 94 11. User-Defined Annotations ......................................... 97 12. The Scala Standard Library ..................................... 101 12.1. Root Classes .......................................... 101 12.2. Value Classes .......................................... 103 12.2.1. Numeric Value Types ................................ . 103 12.2.2. The Boolean Class ................................ . 105 12.2.3. The Unit Class .................................... 106 12.3. Standard Reference Classes ..................................... 106 12.3.1. The String Class .................................. 106 12.3.2. The Tuple Class ................................... 106 12.3.3. The Function Class ................................ 107 12.3.4. The Array Class ................................... 107 12.4. The Node Class ....................................... 109 12.5. The Predef Object ................................... 111 Foreword Scala is a Java-like programming language that combines object-oriented programming and functional programming. Scala is purely object-oriented, every value is an object, and the type and behavior of objects are defined by classes, different classes can be combined through mixin composition. Scala is designed to seamlessly interoperate with the two mainstream object-oriented programming languages, Java and C#, neither of which is purely object-oriented. Scala is also a functional programming language, every function is a value, natively supporting nested function definitions and higher-order functions. Scala also supports a general form of pattern matching, which is used to manipulate algebraic types and is implemented in many functional languages.
← Scala IntroHtml5 Web Sql β†’