SlideShare une entreprise Scribd logo
1  sur  17
Syntax analysis of Scala
Compiler construction
Speaker: Nurgaliev Ildar
Basic Syntax
❖ Scala is case-sensitive
❖ Class names - For all class names, the first letter should be in Upper
Case.
❖ Method Names - All method names should start with a Lower Case letter.
❖ Program File Name - Name of the program file should exactly match the
object name.
Characters are distinguished
according to the following classes
1. Whitespace characters: u0020(space) u0009(tab) u000D(r) u000A(n)
2. Letters: (Unicode categories) lower case letters(Ll), upper case letters(Lu), title-
case letters(Lt), other letters(Lo), letter numerals(Nl) and the two characters
‘$’ and ‘_’
3. Digits: ‘0’ . . . ‘9’
4. Parentheses: ( ) [ ] { }
5. Delimiter characters: ‘ ’ " . ; ,
6. Operator characters: all printable ASCII characters u0020-u007F, mathematical
symbols(Sm) and other symbols(So) except parentheses ([]) and periods
Characters are distinguished
according to the following classes
Ex:
val #^ = 1 // legal - two opchars
val # = 1 // illegal - reserved word like class or => or @
val + = 1 // legal - opchar
val &+ = 1 // legal - two opchars
val &2 = 1 // illegal - opchar and letter do not mix arbitrarily
val £2 = 1 // working - £ is part of Sc (Symbol currency) - undefined by spec
val ¬ = 1 // legal - part of Sm
Scala identifiers
4 types of identifiers supported by Scala:
● ALPHANUMERIC IDENTIFIERS
● OPERATOR IDENTIFIERS
● MIXED IDENTIFIERS
● LITERAL IDENTIFIERS
ALPHANUMERIC IDENTIFIERS
● An ALPHANUMERIC IDENTIFIERS starts with a letter or underscore,
which can be followed by further letters, digits, or underscores.
● '$' character is a reserved keyword
Legal alphanumeric identifiers:
age, salary, _value, __1_value
Illegal identifiers:
$salary, 123abc, -salary
OPERATOR IDENTIFIERS
● An operator identifier consists of one or more operator characters.
● Operator characters are printable ASCII characters such as +, :, ?, ~ or #.
Legal operator identifiers:
+ ++ ::: <?> :>
ps: The Scala compiler will internally "mangle" operator identifiers to turn them into legal Java
identifiers with embedded $ characters. For instance, the identifier :-> would be represented internally
as $colon$minus$greater method. This is consistent with Java anonymous class names.
MIXED IDENTIFIERS
Consists of an alphanumeric identifier, which is followed
by an underscore and an operator identifier.
Legal mixed identifiers: unary_+, myvar_=
unary_+ as a method name defines a unary + operator
myvar_= as method name defines an assignment operator.
MIXED IDENTIFIERS
Scala will only allow mixed identifier names (containing alphanumerics and punctuation) if
you separate them by _.
Illegal identifiers:
scala> def iszero?(x : Int) = x == 0
<console>:1: error: '=' expected but identifier found.
def iszero?(x : Int) = x == 0
^
Legal identifiers:
scala> def iszero_?(x : Int) = x == 0
iszero_$qmark: (x: Int)Boolean
LITERAL IDENTIFIERS
A literal identifier is an arbitrary string enclosed in back-quotes (` . . . `).
Legal literal identifiers:
- `x` `<clinit>` `yield`
- using back-quotes, you can more or less give any name to a field identifier.
val ` ` = 0
Back-quotes are necessary when Java identifiers clash with Scala reserved words:
Thread.`yield`()
LITERAL IDENTIFIERS
- Case statements. The convention is that Lower case names refer to match variables; Upper case names refer to identifiers from the outer
scope.
val A = "a"
val b = "b"
"a" match {
case b => println("b")
case A => println("A")
} //case A were unreachable
"a" match {
case `b` => println("b")
case A => println("A")
} // prints "A".
Scala Keywords:
abstract case catch class
def do else extends
false final finally for
forSome if implicit import
lazy match new null
object override package private
protected return sealed super
this throw trait try
true type val var
while with yield
- : = =>
<- <: <% >:
# @
Comments in Scala
object HelloWorld {
/* This is my first Scala program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
* /* Nested comments are available */
*/
def main(args: Array[String]) {
// Prints Hello World
// This is also an example of single line comment.
println("Hello, world!")
}
}
Newline Characters
Scala is a line-oriented language: statements may be terminated by
semicolons (;) or newlines.
def swap(i: Int, j: Int) {
val t = xs(i)
xs(i) = xs(j)
xs(j) = t()
}
Semicolon is required if you write multiple statements on a single line:
val s = "hello"; println(s)
Scala mode and XML mode
● Start XML scanning when “<” is encountered
● Finish XML scanning after successful parse
● Scala expressions are embeddable in XML
● Need to keep stack of Scala/XML nesting
● No Scala tokens in XML mode
Scala mode and XML mode
var xmlBook =
<book>
<title>Scala</title>
<version>{book.ver}</version>
</book>
References:
- The Scala Language Specification Version 2.9
Martin Odersky, 11 June 2014
- Scala for the impatient
Cay S. Hostman

Contenu connexe

Tendances

Tendances (20)

Variables
VariablesVariables
Variables
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Scala
ScalaScala
Scala
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
Google06
Google06Google06
Google06
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
Scala
ScalaScala
Scala
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
Demystifying Shapeless
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 Training
 
Perl
PerlPerl
Perl
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3
 
Javascript Journey
Javascript JourneyJavascript Journey
Javascript Journey
 

En vedette

으리카지노 ''SX797.COM'' 릴게임설명
으리카지노 ''SX797.COM'' 릴게임설명으리카지노 ''SX797.COM'' 릴게임설명
으리카지노 ''SX797.COM'' 릴게임설명
jsdfweg
 
The Aquarium of Barcelona
The Aquarium of BarcelonaThe Aquarium of Barcelona
The Aquarium of Barcelona
guadi1981
 
Oceanarium Reimagined Activity Teacher Final
Oceanarium Reimagined Activity Teacher FinalOceanarium Reimagined Activity Teacher Final
Oceanarium Reimagined Activity Teacher Final
Casa Thomas Jefferson
 
C:\Users\Magy\Documents\Oceanarium Jacob Kwas
C:\Users\Magy\Documents\Oceanarium  Jacob KwasC:\Users\Magy\Documents\Oceanarium  Jacob Kwas
C:\Users\Magy\Documents\Oceanarium Jacob Kwas
Mrs. Drouare
 
Oceanarium Reimagined Students Sheet
Oceanarium Reimagined Students SheetOceanarium Reimagined Students Sheet
Oceanarium Reimagined Students Sheet
Denise De Felice
 
Shedd Aquarium
Shedd AquariumShedd Aquarium
Shedd Aquarium
Jenny
 
The oceanarium
The oceanariumThe oceanarium
The oceanarium
ESGN
 
Aquarium audit
Aquarium auditAquarium audit
Aquarium audit
Damon89
 
Microorganisms in Marine Environments
Microorganisms in Marine EnvironmentsMicroorganisms in Marine Environments
Microorganisms in Marine Environments
Nagat Abd Elrahim
 

En vedette (20)

Lets explore singapore
Lets explore singaporeLets explore singapore
Lets explore singapore
 
으리카지노 ''SX797.COM'' 릴게임설명
으리카지노 ''SX797.COM'' 릴게임설명으리카지노 ''SX797.COM'' 릴게임설명
으리카지노 ''SX797.COM'' 릴게임설명
 
The Aquarium of Barcelona
The Aquarium of BarcelonaThe Aquarium of Barcelona
The Aquarium of Barcelona
 
Oceanarium Reimagined Activity Teacher Final
Oceanarium Reimagined Activity Teacher FinalOceanarium Reimagined Activity Teacher Final
Oceanarium Reimagined Activity Teacher Final
 
C:\Users\Magy\Documents\Oceanarium Jacob Kwas
C:\Users\Magy\Documents\Oceanarium  Jacob KwasC:\Users\Magy\Documents\Oceanarium  Jacob Kwas
C:\Users\Magy\Documents\Oceanarium Jacob Kwas
 
Marine aquarium decoration and live rocks
Marine aquarium decoration and live rocksMarine aquarium decoration and live rocks
Marine aquarium decoration and live rocks
 
Oceanarium Reimagined Students Sheet
Oceanarium Reimagined Students SheetOceanarium Reimagined Students Sheet
Oceanarium Reimagined Students Sheet
 
Shedd Aquarium
Shedd AquariumShedd Aquarium
Shedd Aquarium
 
Oceanarium
OceanariumOceanarium
Oceanarium
 
The oceanarium
The oceanariumThe oceanarium
The oceanarium
 
The role of zoos and aquariums in wildlife health
The role of zoos and aquariums in wildlife healthThe role of zoos and aquariums in wildlife health
The role of zoos and aquariums in wildlife health
 
Aquarium audit
Aquarium auditAquarium audit
Aquarium audit
 
Microorganisms in Marine Environments
Microorganisms in Marine EnvironmentsMicroorganisms in Marine Environments
Microorganisms in Marine Environments
 
A brief background & development history on klcc
A brief background & development history on klccA brief background & development history on klcc
A brief background & development history on klcc
 
Petronas twin towers malaysia
Petronas twin towers malaysiaPetronas twin towers malaysia
Petronas twin towers malaysia
 
Thesis presentation
Thesis presentationThesis presentation
Thesis presentation
 
Petronas twin tower
Petronas twin towerPetronas twin tower
Petronas twin tower
 
Petronas Towers
Petronas TowersPetronas Towers
Petronas Towers
 
Petronas twin-towers
Petronas twin-towersPetronas twin-towers
Petronas twin-towers
 
klcc
klccklcc
klcc
 

Similaire à Scala syntax analysis

Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
Skills Matter
 

Similaire à Scala syntax analysis (20)

Complete Overview about PERL
Complete Overview about PERLComplete Overview about PERL
Complete Overview about PERL
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
 
Scalar data types
Scalar data typesScalar data types
Scalar data types
 
Introduction to perl_control structures
Introduction to perl_control structuresIntroduction to perl_control structures
Introduction to perl_control structures
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
PERL.ppt
PERL.pptPERL.ppt
PERL.ppt
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
06 ruby variables
06 ruby variables06 ruby variables
06 ruby variables
 
String Interpolation in Scala
String Interpolation in ScalaString Interpolation in Scala
String Interpolation in Scala
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Class 8 - Java.pptx
Class 8 - Java.pptxClass 8 - Java.pptx
Class 8 - Java.pptx
 
Perl names values and variables
Perl names values and variablesPerl names values and variables
Perl names values and variables
 
Unit 1-perl names values and variables
Unit 1-perl names values and variablesUnit 1-perl names values and variables
Unit 1-perl names values and variables
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 

Plus de Ildar Nurgaliev

Plus de Ildar Nurgaliev (7)

Presentation
PresentationPresentation
Presentation
 
CloSapn
CloSapnCloSapn
CloSapn
 
Анализ маркетинговой деятельности ООО “БАРС ГРУП”
Анализ маркетинговой деятельности  ООО “БАРС ГРУП”Анализ маркетинговой деятельности  ООО “БАРС ГРУП”
Анализ маркетинговой деятельности ООО “БАРС ГРУП”
 
Fuzzy logic and application in AI
Fuzzy logic and application in AIFuzzy logic and application in AI
Fuzzy logic and application in AI
 
Kotlin compiler construction (very brief)
Kotlin compiler construction (very brief)Kotlin compiler construction (very brief)
Kotlin compiler construction (very brief)
 
Artificial neural network
Artificial neural networkArtificial neural network
Artificial neural network
 
Social dynamic simulation
Social dynamic simulationSocial dynamic simulation
Social dynamic simulation
 

Dernier

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 

Dernier (20)

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 

Scala syntax analysis

  • 1. Syntax analysis of Scala Compiler construction Speaker: Nurgaliev Ildar
  • 2. Basic Syntax ❖ Scala is case-sensitive ❖ Class names - For all class names, the first letter should be in Upper Case. ❖ Method Names - All method names should start with a Lower Case letter. ❖ Program File Name - Name of the program file should exactly match the object name.
  • 3. Characters are distinguished according to the following classes 1. Whitespace characters: u0020(space) u0009(tab) u000D(r) u000A(n) 2. Letters: (Unicode categories) lower case letters(Ll), upper case letters(Lu), title- case letters(Lt), other letters(Lo), letter numerals(Nl) and the two characters ‘$’ and ‘_’ 3. Digits: ‘0’ . . . ‘9’ 4. Parentheses: ( ) [ ] { } 5. Delimiter characters: ‘ ’ " . ; , 6. Operator characters: all printable ASCII characters u0020-u007F, mathematical symbols(Sm) and other symbols(So) except parentheses ([]) and periods
  • 4. Characters are distinguished according to the following classes Ex: val #^ = 1 // legal - two opchars val # = 1 // illegal - reserved word like class or => or @ val + = 1 // legal - opchar val &+ = 1 // legal - two opchars val &2 = 1 // illegal - opchar and letter do not mix arbitrarily val £2 = 1 // working - £ is part of Sc (Symbol currency) - undefined by spec val ¬ = 1 // legal - part of Sm
  • 5. Scala identifiers 4 types of identifiers supported by Scala: ● ALPHANUMERIC IDENTIFIERS ● OPERATOR IDENTIFIERS ● MIXED IDENTIFIERS ● LITERAL IDENTIFIERS
  • 6. ALPHANUMERIC IDENTIFIERS ● An ALPHANUMERIC IDENTIFIERS starts with a letter or underscore, which can be followed by further letters, digits, or underscores. ● '$' character is a reserved keyword Legal alphanumeric identifiers: age, salary, _value, __1_value Illegal identifiers: $salary, 123abc, -salary
  • 7. OPERATOR IDENTIFIERS ● An operator identifier consists of one or more operator characters. ● Operator characters are printable ASCII characters such as +, :, ?, ~ or #. Legal operator identifiers: + ++ ::: <?> :> ps: The Scala compiler will internally "mangle" operator identifiers to turn them into legal Java identifiers with embedded $ characters. For instance, the identifier :-> would be represented internally as $colon$minus$greater method. This is consistent with Java anonymous class names.
  • 8. MIXED IDENTIFIERS Consists of an alphanumeric identifier, which is followed by an underscore and an operator identifier. Legal mixed identifiers: unary_+, myvar_= unary_+ as a method name defines a unary + operator myvar_= as method name defines an assignment operator.
  • 9. MIXED IDENTIFIERS Scala will only allow mixed identifier names (containing alphanumerics and punctuation) if you separate them by _. Illegal identifiers: scala> def iszero?(x : Int) = x == 0 <console>:1: error: '=' expected but identifier found. def iszero?(x : Int) = x == 0 ^ Legal identifiers: scala> def iszero_?(x : Int) = x == 0 iszero_$qmark: (x: Int)Boolean
  • 10. LITERAL IDENTIFIERS A literal identifier is an arbitrary string enclosed in back-quotes (` . . . `). Legal literal identifiers: - `x` `<clinit>` `yield` - using back-quotes, you can more or less give any name to a field identifier. val ` ` = 0 Back-quotes are necessary when Java identifiers clash with Scala reserved words: Thread.`yield`()
  • 11. LITERAL IDENTIFIERS - Case statements. The convention is that Lower case names refer to match variables; Upper case names refer to identifiers from the outer scope. val A = "a" val b = "b" "a" match { case b => println("b") case A => println("A") } //case A were unreachable "a" match { case `b` => println("b") case A => println("A") } // prints "A".
  • 12. Scala Keywords: abstract case catch class def do else extends false final finally for forSome if implicit import lazy match new null object override package private protected return sealed super this throw trait try true type val var while with yield - : = => <- <: <% >: # @
  • 13. Comments in Scala object HelloWorld { /* This is my first Scala program. * This will print 'Hello World' as the output * This is an example of multi-line comments. * /* Nested comments are available */ */ def main(args: Array[String]) { // Prints Hello World // This is also an example of single line comment. println("Hello, world!") } }
  • 14. Newline Characters Scala is a line-oriented language: statements may be terminated by semicolons (;) or newlines. def swap(i: Int, j: Int) { val t = xs(i) xs(i) = xs(j) xs(j) = t() } Semicolon is required if you write multiple statements on a single line: val s = "hello"; println(s)
  • 15. Scala mode and XML mode ● Start XML scanning when “<” is encountered ● Finish XML scanning after successful parse ● Scala expressions are embeddable in XML ● Need to keep stack of Scala/XML nesting ● No Scala tokens in XML mode
  • 16. Scala mode and XML mode var xmlBook = <book> <title>Scala</title> <version>{book.ver}</version> </book>
  • 17. References: - The Scala Language Specification Version 2.9 Martin Odersky, 11 June 2014 - Scala for the impatient Cay S. Hostman

Notes de l'éditeur

  1. http://www.tutorialspoint.com/scala/scala_basic_syntax.htm
  2. Case Sensitivity - Scala is case-sensitive, which means identifier Hello and hellowould have different meaning in Scala. Class Names - For all class names, the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. Example class MyFirstScalaClass Method Names - All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case. Example def myMethodName() Program File Name - Name of the program file should exactly match the object name. When saving the file you should save it using the object name (Remember scala is case-sensitive) and append '.scala' to the end of the name. (if the file name and the object name do not match your program will not compile). Example: Assume 'HelloWorld' is the object name. Then the file should be saved as'HelloWorld.scala' def main(args: Array[String]) - Scala program processing starts from the main() method which is a mandatory part of every Scala Program.
  3. An alphanumeric identifier starts with a letter or underscore, which can be followed by further letters, digits, or underscores. The '$' character is a reserved keyword in Scala and should not be used in identifiers. Following are legal alphanumeric identifiers: age, salary, _value, __1_value Following are illegal identifiers: $salary, 123abc, -salary
  4. An operator identifier consists of one or more operator characters. Operator characters are printable ASCII characters such as +, :, ?, ~ or #. Following are legal operator identifiers: + ++ ::: <?> :> The Scala compiler will internally "mangle" operator identifiers to turn them into legal Java identifiers with embedded $ characters. For instance, the identifier :-> would be represented internally as $colon$minus$greater.
  5. A mixed identifier consists of an alphanumeric identifier, which is followed by an underscore and an operator identifier. Following are legal mixed identifiers: unary_+, myvar_= Here, unary_+ used as a method name defines a unary + operator and myvar_= used as method name defines an assignment operator.
  6. A literal identifier is an arbitrary string enclosed in back ticks (` . . . `). Following are legal literal identifiers: `x` `<clinit>` `yield` The literal definition of identifiers is useful in two cases. The first case is, when there is already a reserved word of the same name in Scala and you need to use a Java library which does not care about that (and of course, why should it). using backticks, you can more or less give any name to a field identifier. val ` ` = 0 which defines a variable with name (one character of whitespace).
  7. The other use case comes with case statements. The convention is that lower case names refer to match variables, whereas upper case names refer to identifiers from the outer scope. So, val A = "a" val b = "b" "a" match { case b => println("b") case A => println("A") } prints "b" (if the compiler were dumb enough not to fail with saying case A were unreachable). If you want to refer to the originally defined val b, you need to use backticks as a marker. "a" match { case `b` => println("b") case A => println("A") } Which prints "A".
  8. Scala supports single-line and multi-line comments very similar to Java. Multi-line comments may be nested, but are required to be properly nested. All characters available inside any comment are ignored by Scala compiler.
  9. Scala is a line-oriented language where statements may be terminated by semicolons (;) or newlines. A semicolon at the end of a statement is usually optional. You can type one if you want but you don't have to if the statement appears by itself on a single line. On the other hand, a semicolon is required if you write multiple statements on a single line: val s = "hello"; println(s)