boostorg / hana

Your standard library for metaprogramming
http://boostorg.github.io/hana
Boost Software License 1.0
1.7k stars 216 forks source link

Transform the Type in hana::type #326

Open ricejasonf opened 7 years ago

ricejasonf commented 7 years ago

Consider a convenience function to perform operations modifying the contained type in a hana::type without unwrapping it and wrapping it again. Consider the following example:

// Current method
decltype(hana::typeid_(hana::back(std::declval<typename some_tuple_type::type>()))){};

// More expressive
type_transform(some_tuple_type{}, hana::back);

Note: It's important that it strips qualifiers since many functions possibly return references.

Possible Implementation (as discussed in Gitter):

template <typename T, typename F>
constexpr auto type_transform(hana::basic_type<T>, F f) {
  return decltype(hana::typeid_(f(std::declval<T>()))){};
}
viboes commented 7 years ago

I don't understand what you are requesting. Could you show a concrete example of what type_transform will do?

ricejasonf commented 7 years ago

Like hana::transform but for hana::type:

#include <boost/hana.hpp>

namespace hana = boost::hana;

template <typename T, typename F>
constexpr auto type_transform(hana::basic_type<T>, F f) {
    return decltype(hana::typeid_(f(std::declval<T>()))){};
}

int main() {
  constexpr auto foo = hana::type_c<hana::tuple<int, char, double>>;

  static_assert(type_transform(foo, hana::back) == hana::type_c<double>);
}
ricejasonf commented 7 years ago

I'd like to double-down and suggest that hana::type be a Functor.

This would not be unlike hana::experimental::types' implementation as a Functor.