This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
19:07 ~/Projects/Kepler_typemacros/sandbox (topic/typemacros)$ scalac Macros.scala | |
19:07 ~/Projects/Kepler_typemacros/sandbox (topic/typemacros)$ scala -cp . | |
Welcome to Scala version 2.10.1-20121120-175733-b60f5bcf2c (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> class C extends AnyRef with Macros.Foo[Int]("2") | |
defined class C | |
scala> new C().hello |
This file contains hidden or 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 shapeless._, Nat._ | |
trait FizzBuzz[N <: Nat] { | |
type R3 <: Nat | |
type R5 <: Nat | |
def ti: ToInt[N] | |
def prev: List[String] | |
def d3: R3 =:= _0 | |
def d5: R5 =:= _0 |
This file contains hidden or 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
// Searching for large ModAux instances can be extremely slow. | |
// See this version for a faster solution: https://gist.github.com/4108026 | |
import shapeless._, Nat._ | |
trait FizzBuzz[N <: Nat] { | |
def steps: List[String] | |
def show = println(steps.reverse.mkString("\n")) | |
} |
This file contains hidden or 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
/** | |
* Digging through arbitrarily nested case classes, tuples, and lists | |
* by Travis Brown | |
* | |
* In response to this question by Channing Walton on the Shapeless dev list: | |
* | |
* https://groups.google.com/d/msg/shapeless-dev/hn7_U21tupI/Zm9h3uNb51gJ | |
* | |
* Tested with Scala 2.9.2 and Shapeless 1.2.3. Should work on 1.2.2 with minor edits. | |
*/ |
This file contains hidden or 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-level Tower of Hanoi | |
* by Travis Brown | |
* | |
* Note: not optimal, and probably won't work for some valid inputs. | |
* Tested with Scala 2.9.2 and Shapeless 1.2.3. | |
*/ | |
import shapeless._, Nat._ |
This file contains hidden or 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
/** | |
* We can use the Scala type system (with help from Miles Sabin's Shapeless | |
* library) to solve the bank account number validation problem in the second | |
* user story of the KataBankOCR kata on Coding Dojo: | |
* | |
* http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR | |
* | |
* By Travis Brown in response to a question by Paul Snively on the Shapeless | |
* Dev mailing list: | |
* |
This file contains hidden or 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 shapeless | |
import shapeless._ | |
import HList._ | |
package object autoordering extends App { | |
type Iso[T, L <: HList] = HListIso[T, L] | |
implicit def ccOrdering[C, L <: HList](implicit iso: Iso[C, L], L: Ordering[L]) = new Ordering[C] { |