Can Hana’s mechanism for customizing behavior via tag-dispatching be seen as a form of adapter pattern?

tl;dr

Is the customization of boost::hana::transform for std::vector (via specializing boost::hana::tranform_impl for the tag ext::std::vector) a form of Adaptor pattern that wraps the STL’s std::transform into Hana’s interface function boost::hana::transform?

Why this question

I’m reading Dive into Design Patterns, and I’m a bit bothered by the fact that this resource, just like any others I’ve been peeping into (including Head First – Design Patterns), doesn’t have a single page, probably not even a paragraph, that doesn’t contain the words inheritance/virtual/extend/etc, just like inheritance is the only way to understand and use design patterns.

In an attempt to get a less inheritance-biased understanding of what these infamous patterns are, I’m trying to look at them from other perspectives, by asking what they look like in functional programming languages like Haskell (e.g. this question), or, with the present question, if a use of such patters is present in libraries which make use of a lot of template meta-programming, such as Boost.Hana.

The question

If you look at /usr/include/boost/hana/ext/std/vector.hpp, there’s some (commented) code for making std::vector a functor in the way that Hana defines it, i.e. by specializing the transform_impl template for std::vector (well, actually for the tag ext::std::vector which is associated to any std::vector<T>).

The specialization obviously resorts to std::transform (and some template tricks/enable_if) to get the job done.

The net effect, however, is that if I uncomment that code, then I’m able to use boost::hana::transform on std::vector, which I couldn’t do without uncommenting that code.

On the other hand, std::vector is conceptually a functor whether or not I have boost::hana::transform at my disposal. Indeed, std::transform exists in the STL, and the "only" drawback is that it’s not usable in a functional way because it’s iterator-based.

Therefore, it looks to me that customizing boost::hana::transform for std::vector (via specializing boost::hana::tranform_impl for the tag ext::std::vector) is a bit like adapting the STL interface to functors, std::transform, to Hana’s interface, boost::hana::transform.

Is this a valid interpretation?

Source: Windows Questions C++

LEAVE A COMMENT