MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Type level Programming

Examples

Introduction to type-level programming


If we consider a heterogenous list, wherein the elements of the list have varied but known types, it might be desirable to be able to perform operations on the elements of the list collectively without discarding the elements' type information. The following example implements a mapping operation over a simple heterogenous list.

Because the element type varies, the class of operations we can perform is restricted to some form of type projection, so we define a trait Projection having abstract type Apply[A] computing the result type of the projection, and def apply[A](a: A): Apply[A] computing the result value of the projection.

trait Projection {
 type Apply[A] // <: Any
 def apply[A](a: A): Apply[A]
}

In implementing type Apply[A] we are programming at the type level (as opposed to the value level).

Our heterogenous list type defines a map operation parametrized by the desired projection as well as the projection's type. The result of the map operation is abstract, will vary by implementing class and projection, and must naturally still be an HList:

sealed trait HList {
 type Map[P <: Projection] <: HList
 def map[P <: Projection](p: P): Map[P]
}

In the case of HNil, the empty heterogenous list, the result of any projection will always be itself. Here we declare trait HNil as a convenience so that we may write HNil as a type in lieu of HNil.type:

sealed trait HNil extends HList
case object HNil extends HNil {
 type Map[P <: Projection] = HNil
 def map[P <: Projection](p: P): Map[P] = HNil
}

HCons is the non-empty heterogenous list. Here we assert that when applying a map operation, the resulting head type is that which results from the application of the projection to the head value ( P#Apply[H]), and that the resulting tail type is that which results from mapping the projection over the tail (T#Map[P]), which is known to be an HList:
case class HCons[H, T <: HList](head: H, tail: T) extends HList {
 type Map[P <: Projection] = HCons[P#Apply[H], T#Map[P]]
 def map[P <: Projection](p: P): Map[P] = HCons(p.apply(head), tail.map(p))
}

The most obvious such projection is to perform some form of wrapping operation - the following example yields an instance of HCons[Option[String], HCons[Option[Int], HNil]]:

HCons("1", HCons(2, HNil)).map(new Projection {
 type Apply[A] = Option[A]
 def apply[A](a: A): Apply[A] = Some(a)
})



Conclusion

In this page (written and validated by ) you learned about Scala Type level Programming . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala User Defined Functions for Hive.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.