Simplifying development-full - Mirco Dotta (Typesafe)
Upcoming SlideShare
Loading in...5
×
 

Simplifying development-full - Mirco Dotta (Typesafe)

on

  • 492 views

Scala meetup - Milan, 25 May 2013 ...

Scala meetup - Milan, 25 May 2013

Una delle caratteristiche peculiari di Scala consiste nel semplificare e velocizzare la fase di sviluppo del software. Unificando la programmazione orientata agli oggetti con la programmazione funzionale, Scala permette di esprimervi in maniera concisa ed efficace. In questa presentazione saranno introdotte le caratteristiche principali del linguaggio e la sua filosofia, al fine di mostrare come del codice scritto in Scala risulti più semplice, corretto e manutenibile.

Statistics

Views

Total Views
492
Views on SlideShare
412
Embed Views
80

Actions

Likes
0
Downloads
7
Comments
0

3 Embeds 80

http://scala-italy.it 38
http://www.scala-italy.it 23
http://www.javastaff.com 19

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Simplifying development-full - Mirco Dotta (Typesafe) Simplifying development-full - Mirco Dotta (Typesafe) Presentation Transcript

  • ScalaSimplifying DevelopmentScalaDay ItalyMilan, May 25, 2013Mirco DottaSaturday, May 25, 13
  • Is Scala the Java of thefuture?Saturday, May 25, 13
  • • It has basically everything Java has now• It has closures (planned for Java 8)• It has rich interfaces (Java 8 defendermethods), and more• It is completely interoperable and runsabout as fast as JavaSaturday, May 25, 13
  • How is Scala differentfrom Java?Saturday, May 25, 13
  • Concise SyntaxSaturday, May 25, 13
  • public class Time {private final int hours;private final int minutes;public Time(int hours, int minutes) {this.hours = hours;this.minutes = minutes;}public int getHours() { return hours; }public int getMinutes() { return minutes; }}class Time(val hours: Int, val minutes: Int)Saturday, May 25, 13
  • Statically typedbut feels dynamicSaturday, May 25, 13
  • val x = 2type is inferredno semicolonSaturday, May 25, 13
  • Unifies OOP and FPSaturday, May 25, 13
  • Every value is an objectSaturday, May 25, 13
  • List(1,2,3).filter(x => x > 2) //> res: List[Int] = List(3)Anonymous function Int => BooleanList(1, 2, 3).filter(new Function[Int, Boolean] {def apply(x: Int): Boolean = x > 2})///> res: List[Int] = List(3)Functions are “just” objectsSaturday, May 25, 13
  • Everything is anexpressionSaturday, May 25, 13
  • def max(x: Int, y: Int): Int =if (x > y) x else yno return statementval x: Int = {val y = 10val z = 5y + z}blocks evaluate to lastexpressionSaturday, May 25, 13
  • Every operation is amethod callSaturday, May 25, 13
  • val x: Int = 10val y = x + 10same as x.+(10)Saturday, May 25, 13
  • Principles, not RulesSaturday, May 25, 13
  • Users can write their own operatorsPrincipleSaturday, May 25, 13
  • class Complex(val re: Int, val im: Int = 0) {def +(that: Complex) =new Complex(this.re + that.re, this.im + that.im)override def toString = s"$re + $im"}val c1 = new Complex(1, 2) //> c1 : Complex = 1 + 2val c2 = new Complex(2, 2) //> c2 : Complex = 2 + 2val c = c1 + c2 //> c : Complex = 3 + 4default argumentSaturday, May 25, 13
  • • new types can look like built-in ones• “grow the language”Saturday, May 25, 13
  • Powerful collectionsSaturday, May 25, 13
  • case class Time(hours: Int, minutes: Int = 0)val times = List(Time(1), Time(2), Time(12,30), Time(16,15))times.filter(time => time.hours >= 12)//> res: List[Time] = List(Time(12,30), Time(16,15))times.map(time => Time(time.hours + 1, time.minutes))//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),Time(17,15))times.take(2) //> res: List[Time] = List(Time(1,0), Time(2,0))times.groupBy(time => time.minutes) //> res: Map[Int,List[Time]] =Map(30 -> List(Time(12,30)), 15 -> List(Time(16,15)), 0 ->List(Time(1,0), Time(2,0)))times.head //> res: Time = Time(1,0)times.last //> res: Time = Time(16,15)Saturday, May 25, 13
  • For-comprehensionSaturday, May 25, 13
  • • More general than for-loops• Used to iterate, filter, and generate newcollectionsSaturday, May 25, 13
  • for (p <- persons; pr <- p.projects;if pr.overdue) yield p.namemay have any number of generatorsguard construct a new collection of the sametype, element by elementp is in scope for other generatorsSaturday, May 25, 13
  • times.filter(time => time.hours >= 12)//> res: List[Time] = List(Time(12,30), Time(16,15))for(time <- times;if time.hours >= 12) yield time//>res: List[Time] = List(Time(12,30), Time(16,15))times.map(time => Time(time.hours + 1, time.minutes))//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),Time(17,15))for(time <- times)yield Time(time.hours + 1, time.minutes)//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),Time(17,15))Saturday, May 25, 13
  • Desugared to calls to filter, map, andflatMapSaturday, May 25, 13
  • Readily available on any classimplementing those methods!Saturday, May 25, 13
  • TraitsSaturday, May 25, 13
  • • Like Java interfaces, but traits• can have behavior (like Java 8 interfaceswith defender methods)• can have state• enable multiple inheritanceSaturday, May 25, 13
  • public interface Comparable<T> {int compareTo(int o);}trait Comparable[T] {def compareTo(that: T): Int}def <(that: T): Boolean =(this compare that) < 0def >(that: T): Boolean =(this compare that) > 0//... RichInterfaceSaturday, May 25, 13
  • Multiple Inheritance• Traits can mix-in multiple traits• Classes can mix-in multiple traits• Both Class and Trait can inherit at mostfrom one ClassSaturday, May 25, 13
  • trait Bird {def fly: String = "Im flying!"}trait Swimmer {def swim: String = "Im swimming!"}class Fish extends Swimmerclass Duck extends Bird with SwimmerSaturday, May 25, 13
  • Embedding DSLsSaturday, May 25, 13
  • val c = new Complex(1, 2) //> c : Complex = 1 + 2Sum Complex & IntHow could we do it?val c1 = 1 + c //> ???val c1 = c + 1 //> ???Saturday, May 25, 13
  • val c1 = 1 + c //> ???• This doesn’t compile because the type of cis not conform to the type expected by the+ method• In Java there would simply be no way tomake this workSaturday, May 25, 13
  • Implicit ConversionsSaturday, May 25, 13
  • • When there is a type error, the compilerlooks for an implicit that could heal theexpression• You are already used to the idea of typesbeing automatically converted into others• E.g.,Type coercion in Java!int a = 2;double b = a;int is converted into adoubleSaturday, May 25, 13
  • Scala gives you the power of creatingyour own conversionsSaturday, May 25, 13
  • class RichInt(n: Int) {def +(other: Complex) =new Complex(n) + other}Let’s create a class that can sum Int with Complexval c = new Complex(1, 2) //> c : Complex = 1 + 2val c1 = RichInt(1) + c //> c1 : Complex = 2 + 2Saturday, May 25, 13
  • But, we want to writeval c1 = 1 + cAnd notval c1 = RichInt(1) + cSaturday, May 25, 13
  • Implicit conversion!Saturday, May 25, 13
  • implicit def int2richInt(n: Int) = new RichInt(n)val c = new Complex(1, 2) //> c : Complex = 1 + 2val c1 = 1 + c //> c1 : Complex = 2 + 2And the compiler will take care of applying theconversionval c1 = int2richInt(1) + cSaturday, May 25, 13
  • Scala simplifiesdevelopment because...Saturday, May 25, 13
  • Less is MoreSaturday, May 25, 13
  • Few languageconstructs with highabstraction powerSaturday, May 25, 13
  • It’s Fun!Saturday, May 25, 13
  • Good for yourbusiness?Saturday, May 25, 13
  • Saturday, May 25, 13
  • Get Started in 5’http://www.typesafe.com/platform/getstartedSaturday, May 25, 13
  • Thankstwitter: @mircodottaSaturday, May 25, 13