www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static if issue?

reply DLearner <bmqazwsx123 gmail.com> writes:
Consider:
```
void main() {
       int     IntVar1 = 12;

       static if (is(typeof(IntVar1) == int)) {
       } else {
          static assert (false, "Type not int.");
       }
}
```
which compiles correctly.

But the inverse logic:
```
void main() {
       int     IntVar1 = 12;

       static if (is(typeof(IntVar1) != int)) {
          static assert (false, "Type not int.");
       } else {
       }
}
```
fails to compile with:
```
test.d(19): Error: found `!=` when expecting `)`
       static if (is(typeof(IntVar1) != int)) {
                                     ^
test.d(19): Error: found `int` when expecting `)`
       static if (is(typeof(IntVar1) != int)) {
                                        ^
test.d(19): Error: found `)` instead of statement
       static if (is(typeof(IntVar1) != int)) {
                                           ^
test.d(22): Error: declaration expected, not `else`
       } else {
         ^
```

Using DMD64 D Compiler v2.112.0 Windows 10


PS Problem with forum.dlang.org - keeps timing out?
Mar 11
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Wed, Mar 11, 2026 at 03:13:25PM +0000, DLearner via Digitalmars-d-learn
wrote:
[...]
 ```
 void main() {
       int     IntVar1 = 12;
 
       static if (is(typeof(IntVar1) != int)) {
          static assert (false, "Type not int.");
       } else {
       }
 }
 ```
[...] Write it this way instead: ``` static if (!is(typeof(IntVar1) == int)) { ... } ``` --T
Mar 11
next sibling parent DLearner <bmqazwsx123 gmail.com> writes:
On Wednesday, 11 March 2026 at 15:28:51 UTC, H. S. Teoh wrote:
 On Wed, Mar 11, 2026 at 03:13:25PM +0000, DLearner via [...]

 Write it this way instead:

 ```
 	static if (!is(typeof(IntVar1) == int)) { ... }
 ```


 --T
Thanks!
Mar 11
prev sibling parent user1234 <user1234 12.de> writes:
On Wednesday, 11 March 2026 at 15:28:51 UTC, H. S. Teoh wrote:
 On Wed, Mar 11, 2026 at 03:13:25PM +0000, DLearner via 
 Digitalmars-d-learn wrote: [...]
 ```
 void main() {
       int     IntVar1 = 12;
 
       static if (is(typeof(IntVar1) != int)) {
          static assert (false, "Type not int.");
       } else {
       }
 }
 ```
[...] Write it this way instead: ``` static if (!is(typeof(IntVar1) == int)) { ... } ``` --T
Indeed but such an error should not be possible. The front-end should be able to catch that and warn.
Mar 12