SlideShare uma empresa Scribd logo
1 de 28
Baixar para ler offline
Java Forum Stuttgart                                                                 1




                       Scala 2.8.0 – Was gibt’s Neues?

                               Prof. Dr. Oliver Braun
                                         o.braun@fh-sm.de


                                   Fakult¨t Informatik
                                         a
                               Fachhochschule Schmalkalden


                                       01. Juli 2010




                            c 2010 Oliver Braun    Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                     2

¨
Uberblick

            urspr¨nglich nur Collections neu schreiben
                 u
            18 Monate Entwicklungszeit
            parallel viele andere Erneuerungen
                       bessere IDE-Unterst¨tzung
                                           u
                       neue Tools: scalap, scaladoc2
                       Typ-Inferenz f¨r Typ-Konstruktoren
                                     u
                       verbesserte Actors
                       Unterst¨tzung f¨r Continuations
                              u        u
                       ...
            Martin Odersky auf den Scala Days 2010 (sinngem¨ß):
                                                           a
                                es w¨re eigentlich 3.0 angebracht
                                    a
                                  aber 2.8 ist schon announced
                           und wird bereits in B¨chern etc. referenziert
                                                 u


                                 c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                              3

Inhalt



        Named Arguments
        Default Arguments
        Scala 2.8 Collections
        Package-Objekte
        Arrays in Scala 2.8
        Typ-Spezialisierung
        Nested Packages
        Scaladoc 2
        Scala IDE for Eclipse




                          c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                               4

Named Arguments


            Parameter in Funktionsdefinitionen haben Namen, z.B.
            def f ( a : I n t , b : B o o l e a n ) = i f ( b ) a e l s e 0

            diese Namen k¨nnen ab Scala 2.8.0 nicht nur innerhalb der
                           o
            Funktion, sondern auch beim Aufruf der Funktion in der
            Parameterliste genutzt werden, z.B.
             f (a = 7 , b = false )
             f ( b = true , a = 1 2 )
             f (18 , b = true )




                           c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                5

Named Arguments                                                                     (2)


            nicht erlaubt
             f ( b = true , 1 ) // error: positional after named argument
             f ( true , a = 2 ) // error: parameter specified twice: a

            (fast) keine Verwechslung mit Zuweisungen m¨glich
                                                       o
            v ar a = 5
            f ( a = a + 1 , b = f a l s e ) // named argument

            eine Zuweisung der Form a = a + 1 hat den Typ Unit
            bei by-name-Argumenten mit dem Ergebnistyp Unit kann es
            Probleme geben   Compiler-Error



                            c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                               6

Default Arguments



            bei der Definition einer Funktion k¨nnen Default Arguments
                                              o
            angegeben werden, z.B.
            def f ( a : I n t , b : B o o l e a n = t r u e ) =
                       i f (b) a else 0

            damit kann das Argument beim Aufruf weg gelassen werden
             f (7)
             f ( a = 12)




                           c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                 7

Default Arguments                                                                    (2)


            Named und Default Arguments zusammen machen einfache
            ¨
            Uberladung obsolet
             c l a s s R a t i o n a l ( v a l numer : I n t = 0 ,
                                         v a l denom : I n t = 1 )

            4 Konstruktoren“
              ”
            new R a t i o n a l ( ) // 0  1
            new R a t i o n a l ( 2 ) // 2  1
                                                               0
            new R a t i o n a l ( denom = 2 ) //               2
            new R a t i o n a l ( 5 , 3 ) // 53




                             c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                8

Default Arguments                                                                   (3)

            Achtung: Default Arguments ersetzen nicht die
            Implementierung uberladener Methoden
                            ¨
            Beispiel:
            trait      MyTrait {
              def      f ( i : I n t ) : Double
              def      f ( i : I n t , d : Double ) : Double
            }
            class      MyClass extends MyTrait { // e r r o r
              def      f ( i : I n t , d : Double = 1 ) = i ∗ d
            }

            f¨hrt zu
             u
            error: class MyClass needs to be abstract,
                 since method f in trait MyTrait of
                     type (i: Int)Double is not defined
                            c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                     9

copy-Methode f¨r Case-Klassen
              u


     F¨r Case-Klassen wird copy-Methode dank Named und Default
      u
     Arguments automatisch generiert.

     Beispiel:
     case c l a s s C o n f e r e n c e ( name : S t r i n g , y e a r : I n t )

     v a l j f s 2 0 1 0 = C o n f e r e n c e ( ” J a v a Forum S t u t t g a r t ” ,
                                                 2010)

     v a l j f s 2 0 1 1 = j f s 2 0 1 0 copy ( y e a r = 2 0 1 1 )
                        //     Conference(Java Forum Stuttgart,2011)




                              c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                       10

Scala 2.8 Collections


              mit 2.8.0 Redesign der Scala-Collections-Bibliothek
              alle Collection-Klassen sind in scala.collection
              die meisten Basis-Klassen1 existieren in 3 Formen
                 1     in scala.collection.immutable
                 2     in scala.collection.mutable
                 3     in scala.collection
              einzige Ausnahme ist Buffer-Trait (immer mutable)
              die Klassen in scala.collection haben das selbe Interface
              wie die entsprechende in scala.collection.immutable
                       . . . sind aber nicht garantiert unver¨nderbar
                                                             a
              die Klassen in scala.collection.mutable haben zus¨tzlich
                                                               a
              noch Methoden die den Zustand ver¨ndern k¨nnen
                                               a       o

         1
             siehe n¨chste Folie
                    a
                                   c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                           11

Die wichtigsten Collection-Basisklassen


                    Traversable
                         |
                         |
                      Iterable
                         |
         +---------------+-------------------+
         |               |                   |
        Map             Set                 Seq
         |               |                   |
         |          +--------+       +-------+---------+
         |          |        |       |       |         |
     SortedMap SortedSet BitSet Buffer Vector LinearSeq




                       c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                               12

Erzeugen einer Instanz


            . . . durch Angabe des Klassennamens und der Elemente in
            Klammern, z.B.
            T r a v e r s a b l e (1 , 2 , 3)
            Map( ”B” −> ” B e r l i n ” , ”S” −> ” S t u t t g a r t ” )
            S e t ( ” J a v a ” , ”Forum” , ” S t u t t g a r t ” )

            genauso f¨r spezifische Implementierungen
                     u
            L i s t (1 , 2 , 3)
            HashMap ( 1 −> ” one ” , 2 −> ” two ” )

            Repr¨sentation als Zeichenkette mit toString auf die gleiche
                a
            Weise


                           c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                    13

Seq



             Sequenzen sind partielle Funktionen von Int zu dem
             Elementtyp, beginnend bei 0
             Sequenzen definieren eine Methode apply2 zum Indizieren
             Beispiel
             v a l l i s t = L i s t ( ” H a l l o ” , ” Welt ” )
             l i s t ( 1 ) //   Welt




         2
         Ein Ausdruck der Form <objectname>(<parameterlist>) wird in
     Scala immer expandiert zu <objectname>.apply(<parameterlist>)
                                c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                    14

Set

            apply ist identisch zur contains-Methode, d.h.
             val s e t = Set (1 ,2 ,3)
             s e t ( 2 ) // true, entspricht set.contains(2)
             s e t ( 4 ) // false, entspricht set.contains(4)

            Elemente hinzuf¨gen oder entfernen
                           u
                       immutable: Set(1,2) + 3 und Set(1,2) - 2
                       mutable: set += 3 und set -= 2
            Argumentliste mit mehreren Argumenten
             v a l s e t 2 = s e t − ( 3 , 2 ) //                    Set(1)

            Mengenoperationen wie Vereinigung (union, |), Schnitt
            (intersect, &), Mengendifferenz (diff, &~)


                                c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                    15

Map



              apply gibt Wert zur¨ck oder wirft Exception
                                 u
              map ( k e y ) //         value oder Exception

              Lookup-Methode get nutzt den Option-Datentyp3
              def g e t ( k e y ) : O p t i o n [ V a l u e ]

              im Wesentlichen gleiche Methoden wie Set: +, -, +=, -=, . . .




         3
             Option[A] hat die beiden Werte None und Some(x) f¨r ein x: A
                                                              u
                                c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                   16

Map                                                                                    (2)


             Updates mit verschiedener Syntax m¨glich4
                                               o
             map ( 5 ) = ” f i v e ”
             map . u p d a t e ( 5 , ” f i v e ” )
             map + ( 5 −> ” f i v e ” )
                    =

             f¨r immutable Maps
              u
             map . u p d a t e d ( 5 , ” f i v e ” )
             map + ( 5 −> ” f i v e ” )



         4
         Ein Ausdruck der Form
     <objectname>(<parameterlist>) = <expression>
     wird in Scala immer expandiert zu
     <objectname>.update(<parameterlist>, <expression>)
                               c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                17

Migration zu neuen Collection-Klassen

            die in Scala 2.8.0 neu eingef¨hrten Package-Objekte
                                         u
            erleichtern Migration
            package o b j e c t s c a l a {
              type L i s t [+A ] =
                   s c a l a . c o l l e c t i o n . immutable . L i s t [A]
              val L i s t =
                   s c a l a . c o l l e c t i o n . immutable . L i s t
              // . . .
            }

            das Scala-Package-Objekt muss in scala/package.scala
            gespeichert werden
            die dort definierten Member sind dann Teil des Packages
            damit gibt es neben scala.collection.immutable.List
            auch scala.List
                            c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                      18

Arrays in Scala


            Spannungsfeld
                       Interoperabilit¨t mit Java / Effizienz von Java-Arrays
                                      a
            vs.
                       die Vielzahl von Methoden der Scala-Collections soll auch mit
                       Arrays nutzbar sein
            Scala bis 2.7.x nutzt Boxing / Unboxing / Compiler Magic
                       Probleme und zum Teil schlechte Performanz
            auf Scala-Arrays konnten zwar viele Collection-Methoden
            angewendet werden, aber das Ergebnis war kein Array mehr
            gleiches Problem mit String und RichString




                                  c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                      19

Scala 2.8 Arrays


            entsprechen Java Arrays
            implizite Umwandlung in ArrayOps f¨r die
                                              u
            Collection-Methoden ⇒ geben Arrays zur¨ck
                                                   u
            moderen VMs k¨nnen die impliziten Konversionen eliminieren
                         o
              Overhead nahe Null
            zweite implizite Umwandlung in echte“ Seq: WrappedArray
                                          ”
            Auswahl von uberladenen Methoden und Implicits in 2.8.0
                           ¨
            liberalisiert:
                       Priorisierung von Implicits
            Analog: StringOps und WrappedString




                                  c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                               20

Generische Arrays

            Java erlaubt keine Typparameter im Zusammenhang mit
            Arrays
            Scala erlaubt aber beispielsweise
            new A r r a y [ T ] // f¨r T ist Typparameter
                                    u

      ⇒ ben¨tigt Runtime-Information uber T
           o                         ¨
        Mechanismus heisst Manifest
            Manifest[T] kann f¨r bekannte Typen vom Compiler
                                u
            generiert werden und wird als impliziter Parameter ubergeben
                                                               ¨
            abgeschw¨chte Version ClassManifest kann erzeugt werden
                    a
            wenn nur Top-Level-Klasse eines Typen bekannt ist (reicht f¨r
                                                                       u
            Arrays)


                           c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                       21

Generische Arrays                                                                          (2)


            Beispiel
            def l i s t T o A r r a y [T] ( l i s t : L i s t [T] )
                     ( i m p l i c i t m: C l a s s M a n i f e s t [ T ] ) = {
              v a l x s = new A r r a y [ T ] ( l i s t . l e n g t h )
              f o r ( i <− 0 u n t i l l i s t . l e n g t h ) x s ( i ) = l i s t ( i )
              xs
            }

            k¨rzer mit Context Bound
             u
            def l i s t T o A r r a y [T: C l a s s M a n i f e s t ]
                                      ( l i s t : L i s t [ T ] ) = { // . . .




                                c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                   22

Generische Arrays                                                                      (3)




            Funktion die listToArray nutzt und selbst Typparameter hat
            muss auch Manifest bieten:
            d e f mkArray [ T : C l a s s M a n i f e s t ] ( x : T∗ ) =
                listToArray (x . toList )

            GenericArray ist immer Java-Referenz-Array und braucht
            daher kein Manifest




                               c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                               23

Typ-Spezialisierung




            Scalas parametrischer Polymorphismus basiert auf Type
            Erasure
            f¨r primitive Typen heisst das Un-/Boxing
             u
                Unit, Boolean, Byte, Short, Char, Int, Long, Float,
                Double
              ⇒ ca. 10 mal langsamer
              ⇒ Programmierer nehmen keine generischen Collections




                           c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                   24

Typ-Spezialisierung                                                                    (2)



            Ausweg: Type Specialization in Scala 2.8
            c l a s s V e c t o r [@ s p e c i a l i z e d A ] {
                def a p p l y ( i : I n t ) : A = // . . .
                def map [@ s p e c i a l i z e d ( I n t , B o o l e a n ) B ]
                           ( f : A = B) = // . . .
                                        >
            }

            Compiler erzeugt generische Klasse Vector und spezialisierte
            f¨r jeden primitiven Typ
             u
            map wird f¨r Int und Boolean spezialisiert
                      u




                               c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                 25

Nested Packages
            Java hat nur absolute Packages, in Scala k¨nnen Packages
                                                      o
            verschachtelt werden
            Pre-2.8:
            package n e t . o b r a u n
            import j a v a . u t i l . S c a n n e r

            schl¨gt fehl, wenn es ein Sub-Package net.java gibt
                 a
            2.8:
            package n e t . o b r a u n

            sucht nicht in net
            package n e t
            package o b r a u n

            sucht in net
                             c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                             26

Neues Scaladoc


        moderneres Layout
        Tags, wie in Javadoc
               @author
               @param
               @return
               ...
        Wiki-Syntax in
        Sourcecode-
        Kommentaren
        Makros
        @define <name> <body>
        nutzbar als $name


                         c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                              27

Eclipse-Plugin



        gemischte
        Scala/Java-Projekte
        Syntax Highlighting
        Code-Completion
        Hyperlinks zu
        Definitionen
        Error Markers
        Debugging
        ...




                          c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?
Java Forum Stuttgart                                                                 28

Vielen Dank f¨r die Aufmerksamkeit – Fragen?
             u


          02.12.2010
          Die Sprache Scala
          Die Tools
                  Interpreter / Compiler
                  ...
                  Simple Build Tool
                  ScalaDoc / ScalaCheck
                  / ScalaTest / . . .
          Die Frameworks
                  Lift
                  Akka
                  ...



                             c 2010 Oliver Braun   Scala 2.8.0 – Was gibt’s Neues?

Mais conteúdo relacionado

Destaque

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Destaque (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Java Forum Stuttgart 2010: Scala 2.8.0 - Was gibt's Neues?

  • 1. Java Forum Stuttgart 1 Scala 2.8.0 – Was gibt’s Neues? Prof. Dr. Oliver Braun o.braun@fh-sm.de Fakult¨t Informatik a Fachhochschule Schmalkalden 01. Juli 2010 c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 2. Java Forum Stuttgart 2 ¨ Uberblick urspr¨nglich nur Collections neu schreiben u 18 Monate Entwicklungszeit parallel viele andere Erneuerungen bessere IDE-Unterst¨tzung u neue Tools: scalap, scaladoc2 Typ-Inferenz f¨r Typ-Konstruktoren u verbesserte Actors Unterst¨tzung f¨r Continuations u u ... Martin Odersky auf den Scala Days 2010 (sinngem¨ß): a es w¨re eigentlich 3.0 angebracht a aber 2.8 ist schon announced und wird bereits in B¨chern etc. referenziert u c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 3. Java Forum Stuttgart 3 Inhalt Named Arguments Default Arguments Scala 2.8 Collections Package-Objekte Arrays in Scala 2.8 Typ-Spezialisierung Nested Packages Scaladoc 2 Scala IDE for Eclipse c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 4. Java Forum Stuttgart 4 Named Arguments Parameter in Funktionsdefinitionen haben Namen, z.B. def f ( a : I n t , b : B o o l e a n ) = i f ( b ) a e l s e 0 diese Namen k¨nnen ab Scala 2.8.0 nicht nur innerhalb der o Funktion, sondern auch beim Aufruf der Funktion in der Parameterliste genutzt werden, z.B. f (a = 7 , b = false ) f ( b = true , a = 1 2 ) f (18 , b = true ) c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 5. Java Forum Stuttgart 5 Named Arguments (2) nicht erlaubt f ( b = true , 1 ) // error: positional after named argument f ( true , a = 2 ) // error: parameter specified twice: a (fast) keine Verwechslung mit Zuweisungen m¨glich o v ar a = 5 f ( a = a + 1 , b = f a l s e ) // named argument eine Zuweisung der Form a = a + 1 hat den Typ Unit bei by-name-Argumenten mit dem Ergebnistyp Unit kann es Probleme geben Compiler-Error c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 6. Java Forum Stuttgart 6 Default Arguments bei der Definition einer Funktion k¨nnen Default Arguments o angegeben werden, z.B. def f ( a : I n t , b : B o o l e a n = t r u e ) = i f (b) a else 0 damit kann das Argument beim Aufruf weg gelassen werden f (7) f ( a = 12) c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 7. Java Forum Stuttgart 7 Default Arguments (2) Named und Default Arguments zusammen machen einfache ¨ Uberladung obsolet c l a s s R a t i o n a l ( v a l numer : I n t = 0 , v a l denom : I n t = 1 ) 4 Konstruktoren“ ” new R a t i o n a l ( ) // 0 1 new R a t i o n a l ( 2 ) // 2 1 0 new R a t i o n a l ( denom = 2 ) // 2 new R a t i o n a l ( 5 , 3 ) // 53 c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 8. Java Forum Stuttgart 8 Default Arguments (3) Achtung: Default Arguments ersetzen nicht die Implementierung uberladener Methoden ¨ Beispiel: trait MyTrait { def f ( i : I n t ) : Double def f ( i : I n t , d : Double ) : Double } class MyClass extends MyTrait { // e r r o r def f ( i : I n t , d : Double = 1 ) = i ∗ d } f¨hrt zu u error: class MyClass needs to be abstract, since method f in trait MyTrait of type (i: Int)Double is not defined c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 9. Java Forum Stuttgart 9 copy-Methode f¨r Case-Klassen u F¨r Case-Klassen wird copy-Methode dank Named und Default u Arguments automatisch generiert. Beispiel: case c l a s s C o n f e r e n c e ( name : S t r i n g , y e a r : I n t ) v a l j f s 2 0 1 0 = C o n f e r e n c e ( ” J a v a Forum S t u t t g a r t ” , 2010) v a l j f s 2 0 1 1 = j f s 2 0 1 0 copy ( y e a r = 2 0 1 1 ) // Conference(Java Forum Stuttgart,2011) c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 10. Java Forum Stuttgart 10 Scala 2.8 Collections mit 2.8.0 Redesign der Scala-Collections-Bibliothek alle Collection-Klassen sind in scala.collection die meisten Basis-Klassen1 existieren in 3 Formen 1 in scala.collection.immutable 2 in scala.collection.mutable 3 in scala.collection einzige Ausnahme ist Buffer-Trait (immer mutable) die Klassen in scala.collection haben das selbe Interface wie die entsprechende in scala.collection.immutable . . . sind aber nicht garantiert unver¨nderbar a die Klassen in scala.collection.mutable haben zus¨tzlich a noch Methoden die den Zustand ver¨ndern k¨nnen a o 1 siehe n¨chste Folie a c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 11. Java Forum Stuttgart 11 Die wichtigsten Collection-Basisklassen Traversable | | Iterable | +---------------+-------------------+ | | | Map Set Seq | | | | +--------+ +-------+---------+ | | | | | | SortedMap SortedSet BitSet Buffer Vector LinearSeq c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 12. Java Forum Stuttgart 12 Erzeugen einer Instanz . . . durch Angabe des Klassennamens und der Elemente in Klammern, z.B. T r a v e r s a b l e (1 , 2 , 3) Map( ”B” −> ” B e r l i n ” , ”S” −> ” S t u t t g a r t ” ) S e t ( ” J a v a ” , ”Forum” , ” S t u t t g a r t ” ) genauso f¨r spezifische Implementierungen u L i s t (1 , 2 , 3) HashMap ( 1 −> ” one ” , 2 −> ” two ” ) Repr¨sentation als Zeichenkette mit toString auf die gleiche a Weise c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 13. Java Forum Stuttgart 13 Seq Sequenzen sind partielle Funktionen von Int zu dem Elementtyp, beginnend bei 0 Sequenzen definieren eine Methode apply2 zum Indizieren Beispiel v a l l i s t = L i s t ( ” H a l l o ” , ” Welt ” ) l i s t ( 1 ) // Welt 2 Ein Ausdruck der Form <objectname>(<parameterlist>) wird in Scala immer expandiert zu <objectname>.apply(<parameterlist>) c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 14. Java Forum Stuttgart 14 Set apply ist identisch zur contains-Methode, d.h. val s e t = Set (1 ,2 ,3) s e t ( 2 ) // true, entspricht set.contains(2) s e t ( 4 ) // false, entspricht set.contains(4) Elemente hinzuf¨gen oder entfernen u immutable: Set(1,2) + 3 und Set(1,2) - 2 mutable: set += 3 und set -= 2 Argumentliste mit mehreren Argumenten v a l s e t 2 = s e t − ( 3 , 2 ) // Set(1) Mengenoperationen wie Vereinigung (union, |), Schnitt (intersect, &), Mengendifferenz (diff, &~) c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 15. Java Forum Stuttgart 15 Map apply gibt Wert zur¨ck oder wirft Exception u map ( k e y ) // value oder Exception Lookup-Methode get nutzt den Option-Datentyp3 def g e t ( k e y ) : O p t i o n [ V a l u e ] im Wesentlichen gleiche Methoden wie Set: +, -, +=, -=, . . . 3 Option[A] hat die beiden Werte None und Some(x) f¨r ein x: A u c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 16. Java Forum Stuttgart 16 Map (2) Updates mit verschiedener Syntax m¨glich4 o map ( 5 ) = ” f i v e ” map . u p d a t e ( 5 , ” f i v e ” ) map + ( 5 −> ” f i v e ” ) = f¨r immutable Maps u map . u p d a t e d ( 5 , ” f i v e ” ) map + ( 5 −> ” f i v e ” ) 4 Ein Ausdruck der Form <objectname>(<parameterlist>) = <expression> wird in Scala immer expandiert zu <objectname>.update(<parameterlist>, <expression>) c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 17. Java Forum Stuttgart 17 Migration zu neuen Collection-Klassen die in Scala 2.8.0 neu eingef¨hrten Package-Objekte u erleichtern Migration package o b j e c t s c a l a { type L i s t [+A ] = s c a l a . c o l l e c t i o n . immutable . L i s t [A] val L i s t = s c a l a . c o l l e c t i o n . immutable . L i s t // . . . } das Scala-Package-Objekt muss in scala/package.scala gespeichert werden die dort definierten Member sind dann Teil des Packages damit gibt es neben scala.collection.immutable.List auch scala.List c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 18. Java Forum Stuttgart 18 Arrays in Scala Spannungsfeld Interoperabilit¨t mit Java / Effizienz von Java-Arrays a vs. die Vielzahl von Methoden der Scala-Collections soll auch mit Arrays nutzbar sein Scala bis 2.7.x nutzt Boxing / Unboxing / Compiler Magic Probleme und zum Teil schlechte Performanz auf Scala-Arrays konnten zwar viele Collection-Methoden angewendet werden, aber das Ergebnis war kein Array mehr gleiches Problem mit String und RichString c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 19. Java Forum Stuttgart 19 Scala 2.8 Arrays entsprechen Java Arrays implizite Umwandlung in ArrayOps f¨r die u Collection-Methoden ⇒ geben Arrays zur¨ck u moderen VMs k¨nnen die impliziten Konversionen eliminieren o Overhead nahe Null zweite implizite Umwandlung in echte“ Seq: WrappedArray ” Auswahl von uberladenen Methoden und Implicits in 2.8.0 ¨ liberalisiert: Priorisierung von Implicits Analog: StringOps und WrappedString c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 20. Java Forum Stuttgart 20 Generische Arrays Java erlaubt keine Typparameter im Zusammenhang mit Arrays Scala erlaubt aber beispielsweise new A r r a y [ T ] // f¨r T ist Typparameter u ⇒ ben¨tigt Runtime-Information uber T o ¨ Mechanismus heisst Manifest Manifest[T] kann f¨r bekannte Typen vom Compiler u generiert werden und wird als impliziter Parameter ubergeben ¨ abgeschw¨chte Version ClassManifest kann erzeugt werden a wenn nur Top-Level-Klasse eines Typen bekannt ist (reicht f¨r u Arrays) c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 21. Java Forum Stuttgart 21 Generische Arrays (2) Beispiel def l i s t T o A r r a y [T] ( l i s t : L i s t [T] ) ( i m p l i c i t m: C l a s s M a n i f e s t [ T ] ) = { v a l x s = new A r r a y [ T ] ( l i s t . l e n g t h ) f o r ( i <− 0 u n t i l l i s t . l e n g t h ) x s ( i ) = l i s t ( i ) xs } k¨rzer mit Context Bound u def l i s t T o A r r a y [T: C l a s s M a n i f e s t ] ( l i s t : L i s t [ T ] ) = { // . . . c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 22. Java Forum Stuttgart 22 Generische Arrays (3) Funktion die listToArray nutzt und selbst Typparameter hat muss auch Manifest bieten: d e f mkArray [ T : C l a s s M a n i f e s t ] ( x : T∗ ) = listToArray (x . toList ) GenericArray ist immer Java-Referenz-Array und braucht daher kein Manifest c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 23. Java Forum Stuttgart 23 Typ-Spezialisierung Scalas parametrischer Polymorphismus basiert auf Type Erasure f¨r primitive Typen heisst das Un-/Boxing u Unit, Boolean, Byte, Short, Char, Int, Long, Float, Double ⇒ ca. 10 mal langsamer ⇒ Programmierer nehmen keine generischen Collections c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 24. Java Forum Stuttgart 24 Typ-Spezialisierung (2) Ausweg: Type Specialization in Scala 2.8 c l a s s V e c t o r [@ s p e c i a l i z e d A ] { def a p p l y ( i : I n t ) : A = // . . . def map [@ s p e c i a l i z e d ( I n t , B o o l e a n ) B ] ( f : A = B) = // . . . > } Compiler erzeugt generische Klasse Vector und spezialisierte f¨r jeden primitiven Typ u map wird f¨r Int und Boolean spezialisiert u c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 25. Java Forum Stuttgart 25 Nested Packages Java hat nur absolute Packages, in Scala k¨nnen Packages o verschachtelt werden Pre-2.8: package n e t . o b r a u n import j a v a . u t i l . S c a n n e r schl¨gt fehl, wenn es ein Sub-Package net.java gibt a 2.8: package n e t . o b r a u n sucht nicht in net package n e t package o b r a u n sucht in net c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 26. Java Forum Stuttgart 26 Neues Scaladoc moderneres Layout Tags, wie in Javadoc @author @param @return ... Wiki-Syntax in Sourcecode- Kommentaren Makros @define <name> <body> nutzbar als $name c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 27. Java Forum Stuttgart 27 Eclipse-Plugin gemischte Scala/Java-Projekte Syntax Highlighting Code-Completion Hyperlinks zu Definitionen Error Markers Debugging ... c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?
  • 28. Java Forum Stuttgart 28 Vielen Dank f¨r die Aufmerksamkeit – Fragen? u 02.12.2010 Die Sprache Scala Die Tools Interpreter / Compiler ... Simple Build Tool ScalaDoc / ScalaCheck / ScalaTest / . . . Die Frameworks Lift Akka ... c 2010 Oliver Braun Scala 2.8.0 – Was gibt’s Neues?