Subject: Re: [boost] Boost Hana comments
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2014-08-19 22:02:55
On 20/08/2014 12:36, Jeremy Maitin-Shepard wrote:
>>> Why isn't a 2-element List, a 2-element std::tuple, etc. an instance?
>> `hana::list`, `std::tuple` and friends can't be made an instance of the
>> `Product` type class because they would not satisfy its laws.
> From my reading of the description of the Product type class laws, the
> problem you are referring to is the uniqueness requirement on the function
> "make": for a tuple, you could add more than two elements, with the extra
> elements containing arbitrary unused values.
> However, I don't see any practical reason why it is useful to have a
> separate Product type from a Tuple type. Potentially, you could document
> that first and second are only valid for 2-element tuples, and make them
> fail for tuples with more than 2 elements. However, particularly since we
> are just talking about metaprogramming, it seems letting them work for any
> tuple would be better.
I might be taking something out of context here, but if you want to have
arbitrary-length tuples, I think it's usually more typical to define
head and tail operations:
tuple<>::head => undefined
tuple<A>::head => A
tuple<A, ...>::head => A
tuple<>::tail => tuple<>
tuple<A>::tail => tuple<>
tuple<A, B>::tail => tuple<B>
tuple<A, B, ...>::tail => tuple<B, ...>
"first" then becomes "head" and "second" becomes "tail::head".