Boost logo

Boost :

Subject: Re: [boost] [hana]recursive data type?
From: Louis Dionne (ldionne.2_at_[hidden])
Date: 2015-06-25 17:50:54


Larry Evans <cppljevans <at> suddenlink.net> writes:

>
> On 06/24/2015 05:25 AM, Larry Evans wrote:
> > How would one define a recursive data type in hana
> > something described by the type equation:
> >
> > hana_list<T> = hana::nothing | std::pair<T,hana_list<T> >
> >
> > where hana_list<T> is a list of T's terminated by nothing?
> >
> I think this:
>
> https://gist.github.com/cppljevans/f270db4ddeaf8bdd5733
>
> would be a start.

Sorry for the late reply; you had the time to do it yourself!
Indeed, what you wrote would be a good start. Then, you might
want to make your List a model of some concepts, like Iterable:

    namespace boost { namespace hana {
        // This would require `_const<Head, Tail>` to inherit publicly from
        // `_pair<Head, Tail>`.
        template <>
        struct head_impl<List> {
            template <typename Head, typename Tail>
            static constexpr auto apply(_pair<Head, Tail> const& xs) {
                return hana::first(xs);
            }
        };

        template <>
        struct tail_impl<List> {
            template <typename Head, typename Tail>
            static constexpr auto apply(_pair<Head, Tail> const& xs) {
                return hana::second(xs);
            }
        };

        template <>
        struct is_empty_impl<List> {
            template <typename Xs>
            static constexpr auto apply(Xs const& xs) {
                return hana::bool_<!Xs::is_cons>;
            }
        };
    }}

However, note that a recursive implementation of a list might not lead to
the best compile-time performance.

Regards,
Louis


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk