Cannot implicitly convert expression `true` of type `bool` to `Flag`

Paul Backus snarwin at gmail.com
Thu Oct 15 23:25:36 UTC 2020


On Thursday, 15 October 2020 at 21:33:32 UTC, Ali Çehreli wrote:
> void main() {
>   auto a = Flag!"foo".no;     // Fine
>   auto b = No.foo;            // Fine
>
>   // Error: cannot implicitly convert expression `true` of type 
> `bool` to `Flag`:
>   auto c = Flag!"foo"(true);
> }
>
> I think that is a wrong limitation because clearly I am being 
> "explicit" there with a bool value. (I understand the issue but 
> it exposes a limitation of the implementation.) I have to use 
> the ternary operator e.g. after parsing a bool from program 
> arguments:
>
>   bool someFlag;
>   // ...
>   foo(someFlag ? Yes.someFlag : No.someFlag);

An easier way is to use a cast:

     bool someFlag;
     // ...
     foo(cast(Flag!"someFlag") someFlag);

std.conv.to also knows how to handle this conversion:

     foo(someFlag.to!(Flag!"someFlag"));


More information about the Digitalmars-d mailing list