Skip to content

Instantly share code, notes, and snippets.

def cast[T](v: Any): Unit = v.asInstanceOf[T]
def cast1[T](v: Any): T = v.asInstanceOf[T]
val v = BigInt(3)
cast[Double](v) // no exception, there should be one
cast1[Double](v) // exception, but from repl code after cast1 itself
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
import scala.annotation.meta.getter
object FirstAnnotation {
def applyImpl[T: c.WeakTypeTag, Ann: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
import c.universe._
val tpe = weakTypeOf[T]
val annTpe = weakTypeOf[Ann]
@alexarchambault
alexarchambault / IssueGen.scala
Last active August 29, 2015 14:13
Lazy & generics
package gen
import shapeless._, record._, labelled._
trait TC[T]
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
implicit val booleanTC: TC[Boolean] = new TC[Boolean] {}
@alexarchambault
alexarchambault / Dummy.scala
Last active August 29, 2015 14:13
sbt launchconfig issue
import java.util.Locale
import javax.servlet.http.{Cookie, HttpServletResponse}
class Dummy extends xsbti.AppMain {
def run(config: xsbti.AppConfiguration) =
try {
// Creating a dummy HttpServletResponse
// - succeeds from sbt run or sbt console
// - fails through launchconfig using the `dummy` script below (NoClassDefFound exception)
val r = new HttpServletResponse {
@alexarchambault
alexarchambault / Test.scala
Last active August 29, 2015 14:13
LabelledGeneric and tags
import tag.@@
import record._
trait CustomTag
case class Dummy(i: Int @@ CustomTag)
case class DummyTagged(b: Boolean, i: Int @@ CustomTag)
object Test extends App {
val gen = Generic[Dummy]
@alexarchambault
alexarchambault / TC NonLabelled.scala
Last active August 29, 2015 14:13
Lazy, labels, and scalaz.@@
package shapeless
package examples
import scalaz.@@
import labelled._
import record._
trait TC[T] {
def apply(): String
}
@alexarchambault
alexarchambault / Base.scala
Last active August 29, 2015 14:12
Lazy & case object constructors
sealed trait Base0
case class Foo0() extends Base0
case class Bar0() extends Base0
sealed trait Base1
case object Foo1 extends Base1
case object Bar1 extends Base1
object Base {
Generic[Foo0]
@alexarchambault
alexarchambault / Lgen2.scala
Created January 2, 2015 00:48
LabelledGeneric, scala 2.10 (2)
case class Foo0()
case class Foo1(i: Int)
object Lgen2 {
import shapeless._
import language.experimental.macros
// Works with this one, but hard to do anything with it then...
implicit def lgen[T]: shapeless.LabelledGeneric[T] = macro shapeless.GenericMacros.materializeLabelled[T, Nothing]
@alexarchambault
alexarchambault / Lgen.scala
Created January 2, 2015 00:47
LabelledGeneric, scala 2.10
case class Foo0()
case class Foo1(i: Int)
object Lgen {
import shapeless._
LabelledGeneric[Foo0]
LabelledGeneric[Foo1]
}
@alexarchambault
alexarchambault / gist:c104ecffcb6190a546d2
Last active August 29, 2015 14:11
Lazy - should not compile, stackoverflow
trait TC[T] {
def apply(t: T): String
}
object TC {
implicit def hnilTC: TC[HNil] =
new TC[HNil] {
def apply(l: HNil) = "HNil"
}