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
-- Idris type classes actually take any kind of value as a parameter, | |
-- we're not restricted to Sets or parameterised Sets. | |
-- So we actually have 'value classes'. It seems we can use type class | |
-- resolution to make rudimentary decision procedures, then: | |
using (xs : List a) | |
data Elem : a -> List a -> Set where | |
Here : Elem x (x :: xs) | |
There : Elem x xs -> Elem x (y :: xs) |
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
/** Instantiating a trait with a type member in a macro. | |
* | |
* Complete working example by Travis Brown for this Stack Overflow question: | |
* http://stackoverflow.com/q/13795490/334519 | |
*/ | |
import scala.language.existentials | |
import scala.language.experimental.macros | |
trait TypeBuilder { type fieldType } |
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
/** Constructing "singleton types" from compile-time literals | |
* | |
* val x = Example.foo(42) | |
* val y: x.T = 42 // compiles | |
* val z: x.T = 43 // doesn't | |
* | |
*/ | |
import scala.language.experimental.macros | |
import scala.reflect.macros.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
/** Constructing "singleton types" from compile-time literals | |
* | |
* val x = Example.foo(42) | |
* val y: x.T = 42 // compiles | |
* val z: x.T = 43 // doesn't | |
* | |
*/ | |
package s | |
import scala.language.experimental.macros |
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 language.experimental.{ macros => scalaMacros } | |
import reflect.macros.Context | |
package object macros { | |
def safe[A](expr: A) = macro safeImpl[A] | |
def safeImpl[A](c: Context)(expr: c.Expr[A]): c.Expr[A] = { | |
import c.universe._ | |
def isIgnoredStatement(tree: Tree) = tree 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
import language.experimental.macros | |
import language.implicitConversions | |
import scala.reflect.macros.Context | |
import scala.reflect.runtime.universe.Tree | |
class ReflectiveClosure[A, B](val tree: Tree, fn: Function1[A, B]) extends Function1[A, B] { | |
def apply(x: A) = fn(x) | |
} | |
object ReflectiveClosure { |
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
package s | |
object Test { | |
// Observe that x.companion is statically typed such that foo is callable | |
def f1() = { | |
val x = new Foo | |
println(x) // Foo instance | |
println(x.companion) // Foo companion | |
println(x.companion.foo) // I'm 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
13:29 ~/Projects/Kepler_introduce-member/sandbox (topic/introduce-member)$ cat Macros.scala | |
import scala.reflect.macros.Context | |
import language.experimental.macros | |
object Macros { | |
def impl(c: Context)(target: c.Tree, name: c.Tree, code: c.Tree) = { | |
import c.universe._ | |
val Literal(Constant(targetType: Type)) = c.typeCheck(target) | |
val Literal(Constant(methodName: String)) = name | |
val Function(methodParams, methodBody) = code |
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 sbt._ | |
import Keys._ | |
import Build.data | |
object build extends Build { | |
lazy val runAll = TaskKey[Unit]("run-all") | |
lazy val standardSettings = Seq( | |
runAllIn(Compile) | |
) |
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 MyFuncMacro.myfunc | |
object MyFuncExample { | |
def main(args: Array[String]) { | |
val x: MyFunc[Int, Int] = myfunc { a: Int => a } | |
println(x) | |
println(x(42)) | |
} | |
} |