Skip to content

Commit

Permalink
Fix silly mistake in Liftable[Boolean => T]
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Duhem committed Apr 14, 2015
1 parent 0bc2bbe commit 8fd57c7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Expand Up @@ -3,11 +3,13 @@ package org.duhemm.parsermacro.quasiquotes
import org.scalameta.adt.{ Liftables => AdtLiftables }

import scala.reflect.macros.blackbox.Context
import scala.reflect.macros.Universe

import scala.meta.{ Dialect, Input, Token }

trait TokenQuasiquoteLiftables extends AdtLiftables {
val u: scala.reflect.macros.Universe
val u: Universe
val c: Context

import u._

Expand All @@ -28,9 +30,8 @@ trait TokenQuasiquoteLiftables extends AdtLiftables {
q"new _root_.scala.meta.Input { val content: _root_.scala.Array[_root_.scala.Char] = ${new String(input.content)}.toArray }"
}

implicit def liftBool2T[T: Liftable]: Liftable[Boolean => T] = Liftable[Boolean => T] { f =>
val tpe = weakTypeOf[T]
q"(x: $tpe) => if (x) ${f(true)} else ${f(false)}"
implicit def liftBool2T[T : c.WeakTypeTag : Liftable]: Liftable[Boolean => T] = Liftable[Boolean => T] { f =>

This comment has been minimized.

Copy link
@xeno-by

xeno-by Apr 15, 2015

Does this need a typetag now?

This comment has been minimized.

Copy link
@Duhemm

Duhemm Apr 15, 2015

Owner

You're right, the type tag was needed when I wrongly reified the function as

val tpe = weakTypeOf[T]
q"(x: $tpe) => if (x) ${f(true)} else ${f(false)}"

which was obviously wrong. Thanks!

q"(x: Boolean) => if (x) ${f(true)} else ${f(false)}"
}

implicit def liftToken: Liftable[Token] = materializeAdt[Token]
Expand Down
13 changes: 13 additions & 0 deletions quasiquotes/src/test/scala/SimpleTokenQuasiquoteSuite.scala
Expand Up @@ -20,4 +20,17 @@ class SimpleTokenQuasiquoteSuite extends TokenQuasiquoteSuite {
_ isIdentNamed "!"
)
}

test("Integers should be correctly tokenized") {
toks"123" shouldConformTo (
_ isIntValued 123
)
}

test("Negative integers should be correctly tokenized") {
toks"-123" shouldConformTo (
_.isMinus,
_ isIntValued -123
)
}
}
7 changes: 7 additions & 0 deletions quasiquotes/src/test/scala/TokenQuasiquoteSuite.scala
Expand Up @@ -11,6 +11,13 @@ abstract class TokenQuasiquoteSuite extends FunSuite {
case _ => false
}

def isMinus: Boolean = t isIdentNamed "-"

def isIntValued(expected: Int): Boolean = t match {
case t: Literal.Int => t.value(t.prev.isMinus) == expected
case _ => false
}

def isWhitespace: Boolean = t.isInstanceOf[Whitespace]

def isComma: Boolean = t.isInstanceOf[`,`]
Expand Down

0 comments on commit 8fd57c7

Please sign in to comment.