This file contains 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 PHANTOM TYPES EXAMPLES | |
// 1 SIMPLE EXAMPLE | |
trait DoorStatus | |
trait DoorOpened extends DoorStatus | |
trait DoorClosed extends DoorStatus | |
case class Door[Status <: DoorStatus] private () { | |
def close[T >: Status <: DoorOpened](): Door[DoorClosed] = Door[DoorClosed]() |
This file contains 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
trait Acceptable[T] | |
object Acceptable { | |
implicit object LongOK extends Acceptable[Long] | |
implicit object IntOK extends Acceptable[Int] | |
} | |
object A { | |
def f[T : Acceptable](t : T) = t |
This file contains 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
// 1 example | |
class A(name: String) { | |
case class B(nr: Int) | |
def add(first: B, second: B): Int = first.nr + second.nr | |
} | |
val a1 = new A(name = "first") | |
val a2 = new A(name = "second") |
This file contains 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 scalaz.\/ | |
import scalaz.syntax.either._ | |
object Example2 { | |
// This example simulates error handling for a simple three tier web application | |
// | |
// The tiers are: | |
// - the HTTP service | |
// - a user authentication layer | |
// - a database layer |
This file contains 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
/** | |
* Part Zero : 10:15 Saturday Night | |
* | |
* (In which we will see how to let the type system help you handle failure)... | |
* | |
* First let's define a domain. (All the following requires scala 2.9.x and scalaz 6.0) | |
*/ | |
import scalaz._ | |
import Scalaz._ |
NewerOlder