I'm doing a sequence of preprocessing steps on strings and I was thinking of using HList
s to improve the safety of my steps. Some processing steps must be run after others, so I was thinking of encoding that in the type system. My first attempt was:
trait Step
trait Raw extends Step
trait A extends Step
trait B extends Step
trait DependsOnA extends Step
trait DependsOnB extends Step
case class ToBeProcessed[S <: Step](value: String)
object ToBeProcessed {
def raw(input: String): ToBeProcessed[Raw] = ...
def doA(input: ToBeProcessed[Raw]): ToBeProcessed[A] = ...
def doB(input: ToBeProcessed[A]): ToBeProcessed[B] = ...
def doDependsOnA(input: ToBeProcessed[B]): ToBeProcessed[DependsOnA] = ...
def doDependsOnB(input: ToBeProcessed[DependsOnA]): ToBeProcessed[DependsOnB] = ...
}
This works, because it forces me to call everything in the sequence doA
> doB
> doDependsOnA
> doDependsOnB
, which is a possible order in which it works, but:
doC
I must change the types of things that are unrelated to itSo I started reading about type level lists in Haskell and realized I could use that to encode my dependencies. So I started reading on Shapeless HList
s and this popped out:
case class ToBeProcessed[S <: HList](value: String)
object ToBeProcessed {
def raw(input: String): ToBeProcessed[Raw :: HNil] = ...
def doA[S <: HList](input: ToBeProcessed[S]): ToBeProcessed[A :: S] = ...
def doB[S <: HList](input: ToBeProcessed[S]): ToBeProcessed[B :: S] = ...
And to encode the dependency I must have a way to check that A is contained in a given HList:
def doDependsOnA[S <: HList, ???](input: ToBeProcessed[S]): ToBeProcessed[DependsOnA :: S] = ...
}
where in the ???
type I must somehow encode that S
contains A
. I'm still not sure how do I do that. Is this possible?
Shapeless already has a typeclass for proving that a HList
contains a certain type: Selector
. You would use it like this:
import shapeless._, ops.hlist.Selector
def doDependsOnA[S <: HList](input: ToBeProcessed[S])(implicit ev: Selector[S,A]): ToBeProcessed[DependsOnA :: S] = ???
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With