As a result of some helpful answers to a question I posted yesterday about tuples in Scala, I've been looking at Scala HLists. I'd like to re-hash a C++ example from that question to ask another:
In C++ one can implement compile-time recursion terminated using template specialization. I've often done this operating on boost tuples which, like Scala/Haskell HLists are constructed by composing the generic 'cons' type multiple times, once for each relevant type and terminating with null_type. So this:
boost::tuple<int, std::string, float>
is implemented under the hood as:
cons<int, cons<std::string, cons<float, null_type> > >
We can then write a pair of functions that recurse at compile-time over this structure, terminating when the second, more-specialized function matches the final cons type. A simple example, counting the number of elements is shown below:
template<typename T1, typename T2>
void countTupleElements( boost::tuples::cons<T1, T2>& tupleRec, int index, const std::vector<std::string>& vals )
{
return 1 + countTupleElements( tupleRec.tail );
}
template<typename T>
void countTupleElements( boost::tuples::cons<T, boost::tuples::null_type>& tupleRec, int index, const std::vector<std::string>& vals )
{
return 1;
}
Crucially this pattern is often used in circumstances where you want to do something different for each of the various tuple element types (not illustrated in my example): in C++ compile-time recursion is essential as once code is running, the type information is lost for all useful purposes.
My question is, is something similar possible with a Scala HList, e.g.
val example = 1 :: 2.0 :: "Hello" :: "World" :: HNil
I'm aware that Scala, running on the JVM, has reflection - and so presumably this could be implemented using run-time recursion with a function using manifests and pattern matching. But I'm interested in knowing if it's possible to do something similar to the C++ example, using compile-time recursion?