Concat enum of strings into one string
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Tue Aug 14 19:58:47 UTC 2018
On Tuesday, August 14, 2018 8:37:33 AM MDT Andrey via Digitalmars-d-learn
wrote:
> On Tuesday, 14 August 2018 at 14:07:23 UTC, Timoses wrote:
> > Here's one version:
> >
> > template StringEnumValues(alias Enum)
> > {
> >
> > import std.traits : EnumMembers;
> > string[] StringEnumValues()
> > {
> >
> > string[] enumValues;
> >
> > static foreach (member; EnumMembers!Enum)
> >
> > enumValues ~= member;
> >
> > return enumValues;
> >
> > }
> >
> > }
> >
> > import std.string : join;
> > pragma(msg, StringEnumValues!Type.join(" "));
>
> Thank you. Hmm, I thought that standard library already has this
> stuff.
Phobos isn't generally going to have super-specific stuff like a function
for taking an enum type and giving you a string containing the names of all
of its members separated by a single space. Rather, it has building blocks
for doing stuff like that. EnumMembers is one of those building blocks, and
as vit showed in his post, its possible to use map instead of a loop like is
done here. So, getting compact code is often a question of figuring out
which building blocks get you what you want. But it's frequently the case
that you need several traits, functions, and/or templates to get where you
want to go. For metaprogramming, std.traits and std.meta are the main places
to look, whereas for operating on runtime values (or using CTFE instead of
template metaprogramming), std.range and std.algorithm are the main places
to look, though depending on what you're doing, pretty much anywhere in
Phobos could be useful. It's just that std.range and std.algorithm are where
the most generic stuff is (as opposed to something like std.string which is
specific to strings or std.file which has file-specific operations).
At first, you're likely to end up with code that looks more like what
Timoses suggested, and over time, it will become easier to come up with
solutions closer to what vit suggested, and it probably won't be uncommon to
start off with a more verbose solution and then end up with a more compact
one later when you figure out how a particular piece of Phobos can be used
to solve the problem in less space. But there's enough in Phobos (even
std.algorithm alone) that it's not all that hard to not realize that
something that's perfect for your problem is in there. It is usually a
building block towards the solution though rather than a function that does
exactly what you want at the high level.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list