Comment #0 by bearophile_hugs — 2013-08-13T07:03:37Z
Created attachment 1242
Hundreds of usage cases found in dmd/Phobos
In Phobos I have counted about 240 cases (even if some of them are possibly
false positives, see the attach) of usages of the pattern:
!is(T1 == T2)
So I suggest to add to D a handy syntax like this (that in past sometimes I
even have used by mistake):
is(T1 != T2)
It's similar to the handy "!is" and "!in" syntaxes.
- - - - - - - - - - - - - - - - - -
Extra: in some cases you can even think about a comparison, but this is left to
other enhancement requests:
class Klass1 {}
class Klass2 : Klass1 {}
enum bool b1 = is(Klass1 < Klass2);
alias T1 = Tuple!(int, "x");
alias T2 = Tuple!(int);
enum bool b2 = is(T1 < T2);
--------------------------
Comment #1 by timon.gehr — 2013-08-13T07:29:42Z
What is the meaning of is(T1!=T2) ?
Is it
!is(T1==T2)
or
is(T1) && !is(T1==T2)
IMO the way to go is to use T1==T2 and T1!=T2. (In this case, both T1 and T2 should be valid types.)
---
Extra: What would be the difference to is(T1:T2) ? I think this would again make more sense without 'is': T1<T2.
Comment #2 by public — 2013-08-13T08:49:44Z
I like the idea. Both because of consistency and because that '!' near to 'i' is so hard to notice.
@Timon
I don't think "is(T1) && !is(T1==T2)" is any useful. It will make "is(T1 != T2) return false for invalid T1 which is rather surprising. I can't imagine the use case for it.
Comment #3 by timon.gehr — 2013-08-13T09:01:05Z
(In reply to comment #2)
> I like the idea. Both because of consistency and because that '!' near to 'i'
> is so hard to notice.
>
> @Timon
>
> I don't think "is(T1) && !is(T1==T2)" is any useful. It will make "is(T1 != T2)
> return false for invalid T1 which is rather surprising. I can't imagine the use
> case for it.
Are you aware that is(T1==T2) gives false for invalid T1 and error for invalid T2?
Comment #4 by public — 2013-08-13T09:07:06Z
(In reply to comment #3)
> Are you aware that is(T1==T2) gives false for invalid T1 and error for invalid
> T2?
Was aware about the former, not about the latter.
But I don't see how it is relevant. When i compare types via is(T1 == T2), information that matters is "are those two the same?". It does not matter why are they not the same. Same goes for is(T1 != T2), but other way around - "are those types not the same"?
Making "!is(T1 != T2) != is(T1 == T2)" breaks common sense quite a lot in my opinion.
Comment #5 by bearophile_hugs — 2013-08-13T09:36:29Z
(In reply to comment #1)
> What is the meaning of is(T1!=T2) ?
>
> Is it
>
> !is(T1==T2)
>
> or
>
> is(T1) && !is(T1==T2)
The most important use case is to test if two already existing types differ.
> IMO the way to go is to use T1==T2 and T1!=T2. (In this case, both T1 and T2
> should be valid types.)
I agree. But I think this is for another enhancement request. Do you want to open it?
Comment #6 by robert.schadek — 2024-12-13T18:10:15Z