D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15320 - static assert(__traits(compiles, xyz)) considered harmful in unittests
Summary: static assert(__traits(compiles, xyz)) considered harmful in unittests
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: Dragos Carp
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-11 18:42 UTC by Andrei Alexandrescu
Modified: 2016-01-03 14:14 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2015-11-11 18:42:44 UTC
The pattern `static assert(__traits(compiles, xyz))` arised probably as a converse to its negation, e.g.:

static assert( __traits(compiles, emplace(&ss2)));
static assert(!__traits(compiles, emplace(&ss2, SS2.init)));

However, the pattern should be avoided for two reasons:

1. Often it's best to also execute the code and make sure it works as expected

2. When working on the module, a failure of `static assert(__compiles)` does not show the reason in the error messages. This makes code more difficult to work with gratuitously.

We should eliminate this pattern from all of Phobos.
Comment 1 John Colvin 2015-11-11 18:46:07 UTC
You just mean in unittests, right? It's useful (necessary?) elsewhere.
Comment 2 Andrei Alexandrescu 2015-11-11 20:16:59 UTC
(In reply to John Colvin from comment #1)
> You just mean in unittests, right? It's useful (necessary?) elsewhere.

Yah, unittests were the only use case I had in mind. What are others?
Comment 3 John Colvin 2015-11-12 11:22:34 UTC
(In reply to Andrei Alexandrescu from comment #2)
> (In reply to John Colvin from comment #1)
> > You just mean in unittests, right? It's useful (necessary?) elsewhere.
> 
> Yah, unittests were the only use case I had in mind. What are others?

template constraints, sanity checks, only way of implementing some template predicates (IIRC it appears quite a bit in std.range and std.traits for this).
Comment 4 Andrei Alexandrescu 2015-11-12 13:41:58 UTC
(In reply to John Colvin from comment #3)
> (In reply to Andrei Alexandrescu from comment #2)
> > (In reply to John Colvin from comment #1)
> > > You just mean in unittests, right? It's useful (necessary?) elsewhere.
> > 
> > Yah, unittests were the only use case I had in mind. What are others?
> 
> template constraints, sanity checks, only way of implementing some template
> predicates (IIRC it appears quite a bit in std.range and std.traits for
> this).

Nice. Yah, those would stay as they are.