As C++ metaprogramming is functional: is there any way of doing something comparable to any functional programming language's (e.g. Haskell's) let or where construct?
I'm using Boost::MPL but would like to have more structure for longer metafunctions. Splitting into several functions is fine but I'd prefer let/where in some cases.
The code is complex and shows a lot of the power of Haskell. The majority of the source code is comments rather than code. In addition, the code was written as clearly as possible rather than focusing on brevity. Hopefully, this will help the reader follow along.
First, Haskell uses lazy evaluation. For example, the following type is valid and Haskell will not complain about it: It is a polymorphic type, it has a single constructor 'AThing', and it is recursive. Values of this type will always be infinite if fully evaluated. That's okay, and you could use this in a program if you wanted to.
Haskell programmers often wonder whether to use let or where . This seems to be only a matter of taste in the sense of " Declaration vs. expression style ", however there is more to it. It is important to know that let ... in ... is an expression, that is, it can be written wherever expressions are allowed.
Haskell has both more flexibility and more control than most languages. Nothing that I know of beats C's control, but Haskell has everything C does unless you need to control specific bytes in memory. So I call Haskell powerful, rather than just 'good.'
The MPL itself doesn't support let
clauses but some of the libraries built on top of it do. One example is metamonad. As the name suggests it also supports some other higher-level functional concepts (monads). A big drawback is, that metamonad
is not an official part of the Boost distribution.
As far as work-arounds for the MPL go, splitting things into functions and using more namespaces to group them and then import the important symbol into your top-level namespace is probably your best choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With