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> trait Foo[A] { def bar[B](b: B): Foo[A with B] } | |
defined trait Foo | |
scala> def baz[A](foo: Foo[A]) = foo bar 1 | |
baz: [A](foo: Foo[A])Foo[A with Int] | |
scala> def baz[A](foo: Foo[A]): Foo[A with Int] = foo bar 1 | |
<console>:8: error: type mismatch; | |
found : Int(1) | |
required: Nothing |
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 scala.io.Source | |
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
trait Schema | |
object DynamicSchemaMaker extends ReflectionUtils with SchemaParsingUtils { | |
import scala.language.dynamics | |
class DynamicSchema[P <: String] extends Schema with Dynamic { |
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 scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
object Demo { | |
def doIt(xs: Any*) = macro doIt_impl | |
def doIt_impl(c: Context)(xs: c.Expr[Any]*) = { | |
import c.universe._ | |
val fields = xs.map(_.tree).map { | |
case q"${_}(${Literal(Constant(name: String))}).->[${_}](${tup}(..$value))" |
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 scala.annotation.StaticAnnotation | |
import scala.collection.mutable.{ Map => MMap } | |
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
class body(tree: Any) extends StaticAnnotation | |
object Macros { | |
val trees = MMap.empty[String, c.Tree forSome { val c: Context }] |
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
scalaVersion := "2.10.2" | |
libraryDependencies ++= Seq( | |
"net.databinder.dispatch" %% "dispatch-core" % "0.11.0", | |
"net.databinder.dispatch" %% "dispatch-json4s-jackson" % "0.11.0", | |
"net.sf.opencsv" % "opencsv" % "2.0" | |
) |
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 showTree(t: T, depth: Int = 0): Unit = t match { | |
case L(i) => printf("%s- %d\n", " " * depth * 2, i) | |
case F((l, r)) => | |
printf("%s-\n", " " * depth * 2) | |
showTree(l, depth + 1) | |
showTree(r, depth + 1) | |
} | |
// From the question: | |
var z = 0 |
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 scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
def myMatchImpl[A: c.WeakTypeTag, B: c.WeakTypeTag](c: Context)( | |
expr: c.Expr[A] | |
)( | |
patterns: c.Expr[PartialFunction[A, B]] | |
): c.Expr[B] = { | |
import c.universe._ |
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> trait Foo { type X } | |
defined trait Foo | |
scala> trait Bar[A] | |
defined trait Bar | |
scala> class Baz[A: Bar] extends Foo { type X = A } | |
defined class Baz | |
scala> implicit object stringBar extends Bar[String] |
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 Foo | |
case object X extends Foo | |
case class Y(i: Int) extends Foo |
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> trait Foo[A] | |
defined trait Foo | |
scala> def optionalFooFor[A](implicit ev: Foo[A] = null) = Option(ev) | |
optionalFooFor: [A](implicit ev: Foo[A])Option[Foo[A]] | |
scala> implicit object stringFoo extends Foo[String] | |
defined module stringFoo | |
scala> optionalFooFor[String] |