The HList
cons cell, which represents one part of an HList
in linked
list style.
The HList
cons cell, which represents one part of an HList
in linked
list style.
Carries the information about the type of this element, plus the HList
type of the rest of the list.
You can use :+:
to make this HList
longer:
scala> val first = Type1("Value") :+: HNil first: olon.common.HLists.HCons[Type1,olon.common.HLists.HNil] = Type1(Value) :+: HNil scala> Type2("Other Value") :+: first res0: olon.common.HLists.HCons[Type2, olon.common.HLists.HCons[Type1, olon.common.HLists.HNil]] = Type2(Other Value) :+: Type1(Value) :+: HNil
The base trait for HList
s.
The base trait for HList
s. Functions that take HList
s will need a type
parameter subtype of HList
:
def myHListFunction[T <: HList](list: HList) = { println(s"This HList has ${list.length} items!") }
Provides the methods that can be used on an HList
.
Provides the methods that can be used on an HList
. These are set apart
here due to certain issues we can experience otherwise with the type variance
on the :+:
class.
The last element of an HList
.
The last element of an HList
. This is the starting point for an HList
,
and you can use :+:
to start one based on it:
scala> Type1("Value") :+: HNil
res0: olon.common.HLists.HCons[Type1,olon.common.HLists.HNil] = Type1(Value) :+: HNil
The HNil singleton.
Basic support for heterogeneous lists, aka HLists.
An
HList
can be constructed like so:Above, we see that the
HList
preserved the value of the types of its members, otherwise we wouldn't have been able to fetchvalue
andotherValue
, respectively.Trying the same thing with a list won't work:
This is because
value
is not defined inBase
. The inferred type of theList
has to be a common ancestor class or trait ofType1
andType2
, and no such type has avalue
method.