Packages

  • package root
    Definition Classes
    root
  • package intersection

    Using HLists as flat representation of intersection types.

    Using HLists as flat representation of intersection types.

    Overview

    A value of the intersection type A & B can be used both as a value of type A as well as a value of type B. Given the two functions

    def f(x: Int): String = x.toString
    def g(x: Boolean): String = if (x) "hello" else "world"

    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.

    Definition Classes
    root
  • Compose
  • ComposeOps
  • Join
  • LowPrioCompose
  • LowPrioSubtype
  • LowestPrioCompose
  • Merge
  • MergeOps
  • Subtype
  • SubtypeOps
  • WF

object Join extends Serializable

Linear Supertypes
Serializable, Serializable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Join
  2. Serializable
  3. Serializable
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. type Aux[L1 <: HList, L2 <: HList, Out0 <: HList] = Join[L1, L2] { type Out = Out0 }

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. implicit def consJoin1[H, L1 <: HList, L2 <: HList](implicit notIn: ∉[H, L2], m: Join[L1, L2]): Aux[::[H, L1], L2, ::[H, Out]]
  7. implicit def consJoin2[H, L1 <: HList, L2 <: HList, L3 <: HList](implicit wf1: WF[::[H, L1]], wf2: WF[L2], r: shapeless.ops.hlist.Remove.Aux[L2, H, (H, L3)], m: Join[L1, L3]): Aux[::[H, L1], L2, ::[H, Out]]
  8. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int
    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  15. implicit def nilJoin[L <: HList]: Aux[HNil, L, L]
  16. final def notify(): Unit
    Definition Classes
    AnyRef
  17. final def notifyAll(): Unit
    Definition Classes
    AnyRef
  18. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  19. def toString(): String
    Definition Classes
    AnyRef → Any
  20. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped