2.067 Beta: Behavior of enum and ref changed

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 10 01:37:37 PDT 2015


On Tuesday, March 10, 2015 08:19:27 Meta via Digitalmars-d-learn wrote:
> On Tuesday, 10 March 2015 at 07:04:48 UTC, Andre wrote:
> > Hi,
> >
> > following coding raises a compiler error with the beta of 2.067.
> > Is this error intended or not?
> > It is working if I change first line of main to: ulong bits;
> >
> > enum Bits: ulong
> > {
> >     none = 0
> > }
> >
> > bool hasBit(ref ulong rBits, ulong rBit)
> > {
> >     return cast(bool)(rBits & rBit);
> > }
> >
> > void main()
> > {
> >     Bits bits;
> >     hasBit(bits, Bits.none);
> > }
> >
> > function app.hasBit (ref ulong rBits, ulong rBit) is not
> > callable using argument types (Bits, Bits)
> >
> > Kind regards
> > André
>
> It's because enums are not implicitly convertible to their base
> type. It was probably a compiler bug that it worked before. It's
> a regression however, so I'll file an issue in Bugzilla. In the
> meantime you can do:
>
> hasBit(cast(ulong)bits, Bits.none);
>
> Or just use a ulong as you mentioned.

enums _are_ implicitly convertible to their base type. e.g. this compiles
just fine

void main()
{
    enum S : string { a = "hello", b = "world" }
    string s = S.a;
}

It's the base type that isn't implicitly convertible to the enum type.

However, the code in question still shouldn't compile because while a Bits
variable may be implicitly convertible to ulong, it _isn't_ a ulong, so
passing it as a ref argument of type ulong isn't legal. Implicit conversions
aren't used with ref. With ref, the type must match exactly.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list