[Issue 20410] ReplaceTypeUnless replaces enums with their basetype

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 21 12:56:40 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=20410

Simen Kjaeraas <simen.kjaras at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras at gmail.com

--- Comment #1 from Simen Kjaeraas <simen.kjaras at gmail.com> ---
This is only true where the base type of the enum fits one of the other
patterns that ReplaceTypeUnless looks for, e.g. U[], U*, etc. - it will leave
regular enums well enough alone, but string enums and the like will decay to
their base types. Of course, there is a discussion to be had here - should the
type change if I do this?

import std.typecons : ReplaceType;

enum Foo : string { bar = "baz" }

pragma(msg, ReplaceType!(char, int, Foo));

Currently, that'd be immutable(int)[], since Foo is a kind of
immutable(char)[], and I've asked to replace char with int.

While there are arguments to be had on both sides of this, I'd go with treating
enums as opaque, since it's easier to forcibly decay them than filter them out
beforehand.

Should be a simple two lines here:
https://github.com/dlang/phobos/blob/58cb6963fbe54e7dc96ae8b48064e84f1eaf818a/std/typecons.d#L8748

        else static if (is(T[0] == enum))
            alias ReplaceTypeUnless = T;

test case:

@safe unittest
{
    enum Enum : string { foo = "Bar" }
    assert(is(ReplaceType!(char, int, Enum)[0] == Enum));
}

--


More information about the Digitalmars-d-bugs mailing list