Tutorial on Writing Modular Programs in Scala
45 Pages
English
Downloading requires you to have access to the YouScribe library
Learn all about the services we offer

Tutorial on Writing Modular Programs in Scala

-

Downloading requires you to have access to the YouScribe library
Learn all about the services we offer
45 Pages
English

Description

Tutorial on Writing Modular Programs in ScalaMartin Odersky and Gilles Dubochet13 September 2006Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 1 of 45Welcome to the Scala tutorial at JMLC 2006A half-day tutorial on the Scala programming language.A rapid, no-frills, presentation of Scala as a language forwriting modular programs.For Java or related programmers.Be advised, you will work too: this is an interactive hands-ontutorial: get your computer ready!Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 2 of 45Meeting ScalaPattern matchingFunctionsMixinsHigher-order FunctionsThis afternoon’s plan1 Meeting Scala2 Pattern matching3 Functions4 Mixins5 Higher-order FunctionsTutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 3 of 45Meeting ScalaPattern matching Scala as a Java cloneFunctions Pushing the envelopeMixins Hands-onHigher-order FunctionsScala vs. JavaAt first glance, Scala is similar to Java (or C]).or rather everything Java has to offer can be found in Scala.Scala is object-oriented, statically typed, throws exceptions,etc.Scala’s syntax will look familiar to Java programmers.Scala compiles to Java bytecode: it runs on any JVM.Scala even shares Java’s libraries: all classes and methodsdefined as a Java libraries are transparently accessible fromScala code.Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 4 ...

Subjects

Informations

Published by
Reads 31
Language English

Exrait

TtuolWnroaiMongtirirorPladuSnismargtraMalacaydniGllnidOrekset1of45esDuboch
13 September 2006
Martin Odersky and Gilles Dubochet
Tutorial on Writing Modular Programs in Scala
Welcome to the Scala tutorial at JMLC 2006
A half-day tutorial on the Scala programming language.
A rapid, no-frills, presentation of Scala as a language for writing modular programs. For Java or related programmers.
Be advised, you will work too: this is an interactive hands-on tutorial: get your computer ready!
f45TutorialacanSsiamgrrorPaludoMgnitirWnolet2obochesDuGillaydnreksnidOaMtr
gSinetMettaPalachctamnretincsoningFunctionsMixisniHhgreo-drreuFTuOnitsredlacSraMasDleocubankyildGrWtiniMgotirlanoogramsinodularPr
Meeting Scala
1
Pattern matching
2
45oft3he
3
Functions
4
Mixins
Higher-order Functions
5
This afternoon’s plan
4fo4tehcobuDsellGindyaskerOdinrt5
Scala vs. Java
At first glance, Scala is similar to Java (or C]). or rather everything Java has to offer can be found in Scala. Scala is object-oriented, statically typed, throws exceptions, etc. Scala’s syntax will look familiar to Java programmers. Scala compiles to Java bytecode: it runs on any JVM. Scala even shares Java’s libraries: all classes and methods defined as a Java libraries are transparently accessible from Scala code.
rotuolaiTSnacalaMorrgmaisModularPnWriting-rroedFrnutcoisntionsMixinsHighenretctamgnihcnuFeeMngtialScataPo-nnasdpoHeelnveethnghiusePnolcavaJasaalacS
Meeting Scala Pattern matchingScala as a Java clone FunctionsPushing the envelope MixinsHands-on Higher-order Functions The two following classes have the same behaviour. In Java: In Scala: classPrintOptions {classPrintOptions { public static voidmain(String[] args) {defmain(args: Array[String]: Unit) = { System.out.println("Opts selected:"); System.out.println("Opts selected:") for(int= 0; i < args.length; i++)i for(valarg < args) if(].[igsarWitsarstht"("))if")")trWsti(h(arg.sta System.out.println( System.out.println( " "+args[i].substring(1)); " "+arg.substring(1)) } } } } You might notice some similarities. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 5 of 45
MeungFioctatnminchaPalrettniteacSgsScationFuncrderreo-iHhgixsnsniMpeloveenhegtinshuPenolcavaJasaalalritoTuluraMgdotininorWalaMinScramsProgndHaons-
This first section will describe some basic differences between Scala and Java you need to be aware of.
These include syntactic differences, different class member definitions, a purely object model and differences in the class model.
Basic differences
45Dsbuliel6tfocoehnOdeartiandGrsky
54
Syntactic differences
Scala uses anlon-typetier-codinenotation for member or parameter definitions. int age (String name)becomes defage (name: String): Int Semi-colons are optional. There is no pre-defined syntax forforloops.Comprehensions are provided instead. Blocks such as{...}are required only to group statements. Single expressions can be defined outside a block. defx =if(p) aelsebis a legal declaration. All definitions can be arbitrarily nested.
laManicSarsmrPgoulargModitinonWrfo7tehcobuDsellidGankyrsdenOtiarotuTlairno-sdnaHepolevheeningtPushlonevacasaJaacalnoSsrehgdro-uFreitcnctunnsioxiMiHinsaPttremntahcniFgMeetingScala
marnteatFunghitciteeMPalacSgnn-odsaneHopelinixigsHtincsMonnuFroitc-rehedrosaJavaclnsScalaagnhteevnnoPesuih45
Everything is an object
All native types (int,double,bool) are classes, define methods etc., but they are not passed as references, they are subclasses oflVanyA(as opposed to other classes that extendAfnyRe). Arrays are objects and array-specific syntax does not exist (c.f. API). Thevoidpseudo-type is replaced by theUnitclass. Instances ofUnitcan be created with().
lielDsbucoeh8tfodenOtiardGankyrssmargorPMalacSniitinonWrulargModirlauTot
reksaydniGllseuDbochet9of45
A new class hierarchy
oMgnaludorPrmargnSsilacartMaOdinoriaTutritilonWsuPenolceehtgniheHopelnvn-odsanderFunctigher-oraaasaJavoisncSlatcmanghiataPrntexiMsHsnicnuFnoitcSlaitgnMee
dOreksayalaMtrniamsinScalarProgrgnitudoMolaiirWnTorut
Objects can even be created as such, without defining a class. objectScalaextendsLanguage {valcreator = LAMP } Objects replace the singleton pattern, There are no static members, instead objects can be created directly. An object with the same name as a class is calledcompanion moduleand can access private class members.
The object value
54fo01etchboDuesllGindoitciMsnsnixhgiH-oererrdncFuontieMteniSgacalaPtternmatchingFundnaHepolno-salsaSsaccaolJavashinnePuenvegthe
nayksredOnitraMat1heocubsDleildGdoluniMgrWtilanoScalmsinograarPrTurito1of45
Richer class members
Java only allows fields and methods in classes. Scala has a richer semantic for class members. defdefines a method. Parameters are allowed, but optional. deff(a: Int): Stringordeff: Stringare legal definitions. val value can also override a (non Adefines a constant value. parameterized)def. This is required for writing “functional” (i.e. invariant) classes. vardefines a variable, like a Java field. objectandclassare legal members in a class. typemembers also exist but will not be covered in this tutorial.
ingSMeetPattcalahctamnretcnuFgnixiMinsioerghHinso-drreuFcnitnoSscalaasaJavaclonehsuPtgnineeholevHapes-ndon