Skip to content

Commit

Permalink
Infer better type for regular operations on Rule1[_] (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
keddelzz committed Mar 29, 2017
1 parent 2e5dfef commit 7b29ef3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/com/github/zenpie/macrowave/Rule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ sealed abstract class Rule[+T <: HList] {


@compileTimeOnly("Calls to method '*' have to be inside a macro invocation!")
def *[V >: T <: HList] : Rule[List[V] :: HNil] = compileTime
def *[V >: T <: HList](implicit S: SingletonRule[V]) : Rule[List[S.Out] :: HNil] = compileTime

@compileTimeOnly("Calls to method '+' have to be inside a macro invocation!")
def +[V >: T <: HList] : Rule[V :: List[V] :: HNil] = compileTime
def +[V >: T <: HList](implicit S: SingletonRule[V]) : Rule[S.Out :: List[S.Out] :: HNil] = compileTime

@compileTimeOnly("Calls to method '?' have to be inside a macro invocation!")
def ?[V >: T <: HList] : Rule[Option[V] :: HNil] = compileTime
def ?[V >: T <: HList](implicit S: SingletonRule[V]) : Rule[Option[S.Out] :: HNil] = compileTime

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ trait RuleParser extends MacroUtils {
val x = helper(r)
parser.Transform(x, aid, tid)

case q"($r).*[$rTpt]" =>
case q"($r).*[$rTpt]($ev)" =>
val tid = typeOf(tree)
val x = helper(r)
parser.Kleene(x, tid)
case q"($r).+[$rTpt]" =>
case q"($r).+[$rTpt]($ev)" =>
val tid = typeOf(tree)
val x = helper(r)
parser.PClosure(x, tid)
case q"($r).?[$rTpt]" =>
case q"($r).?[$rTpt]($ev)" =>
val tid = typeOf(tree)
val x = helper(r)
parser.Optional(x, tid)
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/com/github/zenpie/macrowave/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package object macrowave {
val HNil = shapeless.HNil

type Prepend[P <: HList, S <: HList] = shapeless.ops.hlist.Prepend[P, S]
type SingletonRule[T] = com.github.zenpie.macrowave.support.SingletonRule[T]

type Rule1[+T] = Rule[T :: HNil]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.zenpie.macrowave.support

import com.github.zenpie.macrowave._

trait SingletonRule[T] {
type Out
}

object SingletonRule extends SingletonRuleLowPriorityImplicits {

type Aux[T, Out0] = SingletonRule[T] {
type Out = Out0
}

implicit val hnil: SingletonRule.Aux[HNil, HNil] = new SingletonRule[HNil] {
type Out = HNil
}

implicit def hcons1[T]: SingletonRule.Aux[T :: HNil, T] = new SingletonRule[T :: HNil] {
type Out = T
}

}

trait SingletonRuleLowPriorityImplicits {

implicit def hconsN[H, T <: HList]: SingletonRule.Aux[H :: T, H :: T] = new SingletonRule[H :: T] {
type Out = H :: T
}

}

0 comments on commit 7b29ef3

Please sign in to comment.