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
object Low { | |
def low = "object Low" | |
def shoot = "missed!" | |
} | |
object High { | |
def high = "object High" | |
def shoot = "bulls eye!" | |
} |
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 T { | |
def me: this.type = this | |
} | |
class W { | |
def woohoo: String = "woohoo" | |
} | |
println((new W with T).me.woohoo) |
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
sealed trait Option[+A] { | |
def isDefined: Boolean | |
def flatten[B](implicit ev: A <:< Option[B]): Option[B] | |
} | |
case object None extends Option[Nothing] { | |
def isDefined = false | |
def flatten[B](implicit ev: Nothing <:< Option[B]): Option[B] = None | |
} |
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 2.7 simple type constraint. This can only constrain a type parameter of this function. | |
// Below, in ListW.sumint28, we can't use this approach because we want to constrain T, | |
// a type param of the enclosing trait. | |
def sumint27A[T <: Int](l: List[T]) : Int = l.reduceLeft((a: Int, b: Int) => a + b) | |
trait IntLike[X] extends (X => Int) | |
object IntLike { | |
implicit val intIntLike: IntLike[Int] = new IntLike[Int] { def apply(x: Int) = identity(x) } | |
} |
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 Buildable[T] { | |
def build: T | |
} | |
trait HeadBuilder extends Buildable[String] { | |
var eyeColor = "brown" | |
var hairColor = "red" | |
def withEyeColor(color: String): this.type = { | |
eyeColor = color |
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
// Named <::< to clearly distinguish from Predef.<:< | |
sealed abstract class <::<[-From, +To] extends (From => To) | |
object <::< { | |
implicit def conforms[A]: A <::< A = new (A <::< A) {def apply(x: A) = x} | |
} | |
sealed abstract class =:=[From, To] extends (From => To) | |
object =:= { | |
implicit def tpEquals[A]: A =:= A = new (A =:= A) {def apply(x: A) = x} | |
} |
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
/** | |
* Type Equivalence Bound | |
*/ | |
sealed abstract class =:=[From, To] extends (From => To) { | |
override def toString = "=:=[From, To]" | |
} | |
object =:= { | |
implicit def typeEquals[A]: A =:= A = new (A =:= A) { | |
def apply(x: A) = x |
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
class Scratch | |
trait PartialApply1Of2[T[_, _], A] { | |
type Apply[B] = T[A, B] | |
type Flip[B] = T[B, A] | |
} | |
trait Cofunctor[F[_]] { | |
def comap[A, B](r: F[A], f: B => A): F[B] | |
} |
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
class Scratch | |
import collection.generic.GenericCompanion | |
trait HasGenericCompanion[S[X] <: Traversable[X]] { | |
def companion: GenericCompanion[S] | |
} | |
object HasGenericCompanion { | |
lazy implicit val StreamHasCompanion: HasGenericCompanion[Stream] = new HasGenericCompanion[Stream] { |
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 java.util.ArrayList | |
object Scratch | |
trait Empty[+E[_]] { | |
def empty[A]: E[A] | |
} | |
object Emptys { | |
def <∅>[E[_], A](implicit e: Empty[E]): E[A] = e.empty |
OlderNewer