__traits(compileError, {})

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 10 22:00:03 PDT 2017


On Monday, September 11, 2017 00:41:12 bitwise via Digitalmars-d wrote:
> On Monday, 11 September 2017 at 00:15:08 UTC, Jonathan M Davis
> > In the case of your suggestion, I'd argue that it would simply
> > be better to improve the compiler-generated error messages if
> > they're not good enough.
>
> I'm suggesting the opposite. I'm saying that the compiler
> messages _are_ good enough, but that some functions in phobos
> (emplace, for example) suppress them by using code like this:
>
> `
> static if(__traits(compiles, { ... }))
>      doIt();
> else
>      static assert("custom error message");
> `
>
> So instead of a custom error message, you could simply run the
> offending code through the compiler and output what it says. The
> compiler's error message would most likely be much better and
> more detailed than the custom one inserted by hand.
>
> So essentially, the above would be refactored like this:
>
> `
> static if(__traits(compiles, { doIt(); }))
>      doIt();
> else
>      static assert("Failed to invoke 'doIt': \n\t" ~
> __traits(compilerError, { doIt(); }));
> `
>
> For the case of emplace() though, I'm wondering if that static if
> even needs to be there at all - maybe not.

Well, if you find anything in a Phobos function that you think is
suboptimal, feel free to create a PR to fix it.

- Jonathan M Davis



More information about the Digitalmars-d mailing list