a value foo: Int & Boolean can be passed to both functions
f and g. Using Scala's intersection types, which are
backed by Java interfaces / classes we cannot obtain a
value of type Int with Boolean. In addition, given an
integer and a boolean, we cannot create such a value at runtime.
This library allows to do both by representing a value which
is of type Int and of type Boolean as the type-level list
Int :: Boolean :: HNil. Merging an integer with a boolean
value simply amounts to storing them in a (h)list.
Usage
Since we use the hlist implementation of shapeless as backing data structure
you need to also include shapeless as dependency and import the hlist
operations:
import shapeless._
import ops.hlist._
Now, given two values of intersection types we can apply merge them
to obtain the intersection:
val x: Int :: HNil = 42 :: HNil
val y: Boolean :: HNil = false :: HNil
val both: Int :: Boolean :: HNil = x & y
println(f(both.select[Int]) + " " + g(both.select[Boolean]))
As you notice, the projection into the intersection type has to be
explicitly selected at the moment. The same is true for subtyping
relationships between intersection types:
val y2: Boolean :: HNil = subsume[Boolean :: HNil, Int :: Boolean :: HNil](both)
We hope to work around this issue in future releases.
Implementation
The implementation keeps the invariant, that a type may only occur once
in an intersection (also see wellformedness, WF). Also the order
of elements in the HLists can be arbitrary, effectively
implementing type level sets.
Design decision: Right now, also emtpy HLists are considered wellformed
intersection types. The alternative would be to only allow singleton
allow for empty intersections (see code which is commented out).
We also don't account for subtypes, yet. So if B <: A and we have a list
of type L = B :: HNil, we cannot select an A from it.
This is a consequence of Selector being defined invariant in shapeless.
Using HLists as flat representation of intersection types.
Overview
A value of the intersection type
A & Bcan be used both as a value of typeAas well as a value of typeB. Given the two functionsa value
foo: Int & Booleancan be passed to both functionsfandg. Using Scala's intersection types, which are backed by Java interfaces / classes we cannot obtain a value of typeInt with Boolean. In addition, given an integer and a boolean, we cannot create such a value at runtime.This library allows to do both by representing a value which is of type
Intand of typeBooleanas the type-level listInt :: Boolean :: HNil. Merging an integer with a boolean value simply amounts to storing them in a (h)list.Usage
Since we use the hlist implementation of shapeless as backing data structure you need to also include shapeless as dependency and import the hlist operations:
Now, given two values of intersection types we can apply merge them to obtain the intersection:
As you notice, the projection into the intersection type has to be explicitly selected at the moment. The same is true for subtyping relationships between intersection types:
We hope to work around this issue in future releases.
Implementation
The implementation keeps the invariant, that a type may only occur once in an intersection (also see wellformedness, WF). Also the order of elements in the HLists can be arbitrary, effectively implementing type level sets.
Design decision: Right now, also emtpy HLists are considered wellformed intersection types. The alternative would be to only allow singleton allow for empty intersections (see code which is commented out).
We also don't account for subtypes, yet. So if
B <: Aand we have a list of typeL = B :: HNil, we cannot select anAfrom it. This is a consequence of Selector being defined invariant in shapeless.