Renaming Flag!"" in API

user1234 user1234 at 12.de
Mon Oct 12 10:47:04 UTC 2020


On Monday, 12 October 2020 at 10:24:44 UTC, FreeSlave wrote:
> Let's say I use Flag type named 'myflagname' in API like this:
>
> import std.typecons;
>
> void func(Flag!"myflagname" flag)
> {
> //...
> }
>
> void main()
> {
>     func(Yes.myflagname);
> }
>
> Later I realize that 'myflagname' is a bad name and I want to 
> change it to something else. But if I do so, I break the 
> existing code using this API as Flag with different name will 
> be a different type and Yes.myflagname and No.myflagname won't 
> fit in. I can't use alias as Yes and No relies on string 
> literal.
> Can this issue overcome somehow? Looks like a fundamental flaw 
> with std.typecons.Flag.

Once encountered a similar situation. Flag was good but once you 
know your API, and as in my case I was the only user, I just 
replaced with a template param + constraint.
In the worst case, let's say you forget a bit the API, a DDOC 
popup can help.

example:

---
import std.typecons;

/* Params:  T = anything convertible to bool
             t = indicates whether do this or do that  */
void func(T)(T t) if (is(T : bool)){}

void main()
{
     enum yn { yes = true, no = false}
     // all good, all same semantic
     func(yn.yes);
     func(true);
     func(Yes.myflagname);
}
---


More information about the Digitalmars-d-learn mailing list