This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| scala> case class Foo(n: Int) | |
| defined class Foo | |
| scala> List[Foo]().sorted | |
| <console>:10: error: No implicit Ordering defined for Foo. | |
| List[Foo]().sorted | |
| ^ | |
| scala> implicit def ordFoo = Ordering.by[Foo, Int](_.n) | |
| ordFoo: scala.math.Ordering[Foo] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Animal(val name: String) { | |
| override def equals(other: Any): Boolean = other match { | |
| case that: Animal => | |
| (that canEqual this) && | |
| (this.name == that.name) | |
| case _ => false | |
| } | |
| def canEqual(other: Any): Boolean = other.isInstanceOf[Animal] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| scala> scala.util.Properties.versionString | |
| res0: String = version 2.10.3 | |
| scala> scala.util.Properties.jdkHome | |
| res1: String = /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class Foo { | |
| val a: String | |
| val b = "hello" + a | |
| } | |
| class Bar extends Foo { | |
| val a = "world" | |
| } | |
| // What is wrong with the above code? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // functional typeclass pattern | |
| // using type traits and implicit parameters | |
| case class Person(name: String, age: Int) | |
| case class Restaurant(name: String, location: String) | |
| // Typeclass | |
| trait Serializable[T] { | |
| def ser(t: T): String | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Pred[A] = A => Boolean | |
| def not[A](p: Pred[A]): Pred[A] = a => !p(a) | |
| def and[A](p1: Pred[A], p2: Pred[A]): Pred[A] = a => p1(a) && p2(a) | |
| def or[A](p1: Pred[A], p2: Pred[A]): Pred[A] = a => p1(a) || p2(a) | |
| def isDivisibleBy(k: Int): Pred[Int] = i => i % k == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| scala> :pa | |
| class Outer { | |
| trait Inner // nested type | |
| def y = new Inner {} | |
| def foo(x: Inner) = null // path-dependent type - same as `def foo(x: this.Inner) = null` | |
| def bar(x: Outer#Inner) = null // type projection | |
| } | |
| scala> val x = new Outer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| scala> def f[M[_]](x: M[Int]) = x | |
| f: [M[_]](x: M[Int])M[Int] | |
| scala> val cb = (x: Int) => println(x) | |
| cb: Int => Unit = <function1> | |
| scala> f[Function1](cb) | |
| <console>:11: error: Function1 takes two type parameters, expected: one | |
| f[Function1](cb) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // First-order parametric polymorphism - abstract over types (simple types or proper types) | |
| // Higher-order parametric polymorphism - abstract over type constructors (type functions) | |
| // Other names for Higher-order polymorphism | |
| // - Type constructor polymorphism | |
| // - Higher-kinded types | |
| // Higher-kinded types enables us to use type constructor as type parameters and abstract type members | |
| // Higher-kinded types is a type that abstract over types that abstract over types |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.util.Try | |
| implicit class TryHarder[T](t:Try[T]) { | |
| def harder(f: => Unit) = {f; t} | |
| } | |
| Try(1/0).harder(println("finally")) // prints 'finally' | |
| Try(try { ??? } finally { 1 / 0 }).harder(println("finally finally")) // prints 'finally finally' |