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
// See http://lampsvn.epfl.ch/trac/scala/ticket/967. | |
// | |
// Gilles' fix causes the definition of Nat to be rejected with the error | |
// "Parameter type in structural refinement may not refer to a type member | |
// of that refinement". However we can work around the problem by | |
// quantifying out the problematic parameter type and reinstating it via | |
// a generalized type constraint. | |
type Num = { | |
type Rep |
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 Tuples { | |
import HLists._ | |
implicit def tuple1ToHList[A](t : Product1[A]) = new { def hlisted : A :: HNil = t._1 :: HNil } | |
implicit def tuple2ToHList[A, B](t : Product2[A, B]) = new { def hlisted : A :: B :: HNil = t._1 :: t._2 :: HNil } | |
implicit def tuple3ToHList[A, B, C](t : Product3[A, B, C]) = new { def hlisted : A :: B :: C :: HNil = t._1 :: t._2 :: t._3 :: HNil } | |
implicit def hListToTuple1[A](h : A :: HNil) = new { def tupled : Tuple1[A] = Tuple1(h.head) } | |
implicit def hListToTuple2[A, B](h : A :: B :: HNil) = new { def tupled : (A, B) = (h.head, h.tail.head) } | |
implicit def hListToTuple3[A, B, C](h : A :: B :: C :: HNil) = new { def tupled : (A, B, C) = (h.head, h.tail.head, h.tail.tail.head) } |
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
// Is this even faintly novel? The closest I've seen is | |
// | |
// http://stackoverflow.com/questions/2618891/using-lazy-evaluation-functions-in-varargs | |
// | |
// which is a bit clunky by comparison. But this is so trivial someone must have | |
// done it this way before. | |
// UPDATE: | |
// Thanks to the Twittersphere (@etorreborre, @pchiusano and @loverdos) for a few sightings of related things, | |
// |
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
def unexpected : Nothing = sys.error("Unexpected invocation") | |
// Encoding for "A is not a subtype of B" | |
trait <:!<[A, B] | |
// Uses ambiguity to rule out the cases we're trying to exclude | |
implicit def nsub[A, B] : A <:!< B = null | |
implicit def nsubAmbig1[A, B >: A] : A <:!< B = unexpected | |
implicit def nsubAmbig2[A, B >: A] : A <:!< B = unexpected |
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
case class ServiceId[T](id : Int) | |
implicit val lisServiceId = | |
ServiceId[android.view.LayoutInflater](Context.LAYOUT_INFLATER_SERVICE) | |
def service[T](implicit sid: ServiceId[T]): T = { | |
context.getSystemService(sid.id).asInstanceOf[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
trait Companion[T] { | |
type C | |
def apply() : C | |
} | |
object Companion { | |
implicit def companion[T](implicit comp : Companion[T]) = comp() | |
} | |
object TestCompanion { |
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
// See this mailing list thread for context: | |
// https://groups.google.com/d/topic/scala-language/ImqJuGylyi8/discussion | |
case class Companion[-C, T](t : T) | |
trait Publish[C, T] { self : T => | |
implicit val companion = Companion[C, T](this) | |
} | |
trait StaticKey { |
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 MonoidExamples { | |
import shapeless._ | |
import HList._ | |
trait Monoid[T] | |
// Boilerplate ... | |
trait HListed[S, L <: HList] | |
implicit def hl1[S[_], A] = new HListed[S[A], A :: HNil] {} | |
implicit def hl2[S[_, _], A, B] = new HListed[S[A, B], A :: B :: HNil] {} |
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
// See http://research.microsoft.com/en-us/um/people/simonpj/papers/boxy/boxy-icfp.pdf | |
object Impredicative extends App { | |
import shapeless._ | |
import TypeOperators._ | |
object head extends (List ~> Id) { | |
def default[T](l : List[T]) = l.head | |
} | |
def g(o : Option[List ~> Id]) = o match { |
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
// K ~?> V witnesses the existence of a natural transformation between K and V | |
class ~?>[K[_], V[_]] { | |
class λ[KT, VT] // Witness K ~?> V at a particular K[T], V[T] | |
} | |
object ~?> { | |
// To instantiate the first-order witness we need a value of the | |
// higher-kinded witness, so we depend on it implicitly | |
implicit def witness[K[_], V[_], T](implicit rel : K ~?> V) = new rel.λ[K[T], V[T]] |
OlderNewer