On Github jaceklaskowski / scala-traits-slides
Delivered by Jacek Laskowski / @jaceklaskowski
This presentation is a mere summary a mere summary of what I could learn from the many sources available on the Internet. I wholeheartedly apologise for being incompetent to yield added value besides the presentation itself.
by Martin Odersky, Lex Spoon, and Bill Venners December 10, 2008http://www.artima.com/pins1ed/traits.html
package scala // See http://www.scala-lang.org/api/current/#scala.Function1 trait Function1[-T1, +R] extends AnyRef { def apply(v1: T1): R }
class F_Int2Int extends Function1[Int, Int] { def apply(x: Int): Int = x + 1 }
scala> val f = new Function1[Int, Int] { | def apply(x: Int): Int = x + 1 | } f: Function1[Int,Int] = $anon$1@59020d40 scala> f(0) res0: Int = 1
package scala import scala.compat.Platform.currentTime import scala.collection.mutable.ListBuffer trait App extends DelayedInit { protected def args: Array[String] = ... def main(args: Array[String]) = { ... } }
object Main extends App { Console.println("Hello World: " + (args mkString ", ")) }
// from Akka's akka.actor package trait ActorLogging { this: Actor ⇒ private var _log: LoggingAdapter = _ def log: LoggingAdapter = { // only used in Actor, i.e. thread safe if (_log eq null) _log = akka.event.Logging(context.system, this) _log } }
trait Speakable { def speak() = { println("I am indeed speaking") } }
scala> val s = new Speakable {} s: Speakable = $anon$1@22f195ea scala> s.speak I am indeed speaking
class Human extends Speakable { override def speak() = { println("Hello, a Human speaking") } }
scala> val h = new Human h: Human = Human@962345b scala> h.speak Hello, a Human speaking
scala> val s: Speakable = new Human s: Speakable = Human@65c7d581 scala> s.speak Hello, a Human speaking
class Animal trait CanWalk class Human extends Animal with Speakable with CanWalk { override def speak() = { println("Hello, a Human speaking") } }
trait CanWalk trait CanSing class Living extends CanWalk with CanSing
Spray-Swagger brings Swagger support for Spray APIs.https://github.com/gettyimages/spray-swagger
trait B trait A { this: B => }
trait B trait A extends B
trait A[Self] {this: Self => } // correct trait A[Self] extends Self // incorrect
trait A { self: B => } trait B { self: A => }
trait A extends B trait B extends A error: illegal cyclic reference involving trait A
trait Foo { this => { def close: Unit } }
trait AB { def fa: String def fb: String } trait A1 extends AB { override def fa = "A's String" } trait B1 extends AB { override def fb = "B's String" } val myObj = new A1 with B1
trait A{ def a = 1 } trait X extends A{ override def a = { println("X") super.a } } trait Y extends A{ override def a = { println("Y") super.a } } scala> val xy = new AnyRef with X with Y xy: java.lang.Object with X with Y = $anon$1@6e9b6a scala> xy.a Y X res0: Int = 1 scala> val yx = new AnyRef with Y with X yx: java.lang.Object with Y with X = $anon$1@188c838 scala> yx.a X Y res1: Int = 1
trait SayHello { def hello(s: String): String } trait SayStarredHello extends SayHello { abstract override def hello(s: String): String = "***" + super.hello(s) } trait SayTildedHello extends SayHello { abstract override def hello(s: String): String = "~~~" + super.hello(s) } class ReallySayHello extends SayHello { def hello(s: String): String = "Hello, " + s } // What's the output? (new ReallySayHello with SayStarredHello with SayTildedHello).hello("Jacek")
scala> (new ReallySayHello with SayStarredHello with SayTildedHello).hello("Jacek") res0: String = ~~~***Hello, Jacek
trait Greeting { val name: String val msg = "How are you, "+name } class C extends { val name = "Bob" } with Greeting { println(msg) } // What's the output of the following line? new C
trait Comparable[T <: Comparable[T]] { self: T => def < (that: T): Boolean def <=(that: T): Boolean = this < that || this == that def > (that: T): Boolean = that < this def >=(that: T): Boolean = that <= this }
class AA(val n: Int) extends Comparable[AA] { def < (that: AA) = n < that.n }
scala> val one = new AA(1) one: AA = AA@67a667d6 scala> val two = new AA(2) two: AA = AA@20e164a1 scala> one < two res5: Boolean = true scala> two < one res6: Boolean = false
trait PrintedComparable[T <: Comparable[T]] extends Comparable[T] { self: T => abstract override def < (that: T) = { println(s"Executing $this < $that") super.<(that) } }
scala> :help ... :javap <path|class> disassemble a file or class name scala> :javap :javap [-lcsvp] [path1 path2 ...] scala> :javap -help usage :javap [opts] [path or class or -]... -help Prints this help message -raw Don't unmangle REPL names -app Show the DelayedInit body of Apps -fun Show anonfuns for class or Class#method -verbose/-v Stack size, number of locals, method args -private/-p Private classes and members -package Package-private classes and members -protected Protected classes and members -public Public classes and members -l Line and local variable tables -c Disassembled code -s Internal type signatures -sysinfo System info of class -constants Static final constants
scala> trait A defined trait A scala> :javap -public A Compiled from "<console>" public interface A { }
scala> class Person(name: String) defined class Person
scala> :javap -public Person Compiled from "<console>" public class Person { public Person(java.lang.String); }
scala> class Person(val name: String) defined class Person
scala> :javap -public Person Compiled from "<console>" public class Person { public java.lang.String name(); public Person(java.lang.String); }
scala> class Person(var name: String) defined class Person
scala> :javap -public Person Compiled from "<console>" public class Person { public java.lang.String name(); public void name_$eq(java.lang.String); public Person(java.lang.String); }
scala> case class Person(name: String) defined class Person
scala> :javap -public Person Compiled from "<console>" public class Person implements scala.Product,scala.Serializable { public java.lang.String name(); public Person copy(java.lang.String); public java.lang.String copy$default$1(); public java.lang.String productPrefix(); public int productArity(); public java.lang.Object productElement(int); public scala.collection.Iterator<java.lang.Object> productIterator(); public boolean canEqual(java.lang.Object); public int hashCode(); public java.lang.String toString(); public boolean equals(java.lang.Object); public Person(java.lang.String); }