www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is(this : myClass)

reply Patrick <tengai650 gmail.com> writes:
The compiler seems to reject the following code in a class method:

bool test = is(this : myClass);

Could some please explain this?

Thanks,

Patrick
Oct 20 2017
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, October 20, 2017 21:32:48 Patrick via Digitalmars-d-learn wrote:
 The compiler seems to reject the following code in a class method:

 bool test = is(this : myClass);

 Could some please explain this?
"this" is not a type. is(T : U) is true if T is implicitly convertible to U. T and U must both be types. So, you need to use the types of this and myClass, even if that's just is(typeof(this) : typeof(myClass)) rather than explicitly using their types. - Jonathan M Davis
Oct 20 2017
next sibling parent reply Patrick <tengai650 gmail.com> writes:
On Friday, 20 October 2017 at 21:42:32 UTC, Jonathan M Davis 
wrote:
 On Friday, October 20, 2017 21:32:48 Patrick via 
 Digitalmars-d-learn wrote:
 The compiler seems to reject the following code in a class 
 method:

 bool test = is(this : myClass);

 Could some please explain this?
"this" is not a type. is(T : U) is true if T is implicitly convertible to U. T and U must both be types. So, you need to use the types of this and myClass, even if that's just is(typeof(this) : typeof(myClass)) rather than explicitly using their types. - Jonathan M Davis
Thank you. Due to the very specific nature of the 'is' operator, why wouldn't the compiler know to implicitly query the class types? Why must it be explicitly written, typeof(this)? Patrick
Oct 20 2017
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/20/17 5:55 PM, Patrick wrote:
 Due to the very specific nature of the 'is' operator, why wouldn't the 
 compiler know to implicitly query the class types? Why must it be 
 explicitly written, typeof(this)?
The compiler generally doesn't "fix" errors for you, it tells you there is a problem, and then you have to fix it. You have to be clear and unambiguous to the compiler. Otherwise debugging would be hell. -Steve
Oct 20 2017
parent reply Patrick <tengai650 gmail.com> writes:
On Friday, 20 October 2017 at 22:15:36 UTC, Steven Schveighoffer 
wrote:
 On 10/20/17 5:55 PM, Patrick wrote:
 Due to the very specific nature of the 'is' operator, why 
 wouldn't the compiler know to implicitly query the class 
 types? Why must it be explicitly written, typeof(this)?
The compiler generally doesn't "fix" errors for you, it tells you there is a problem, and then you have to fix it. You have to be clear and unambiguous to the compiler. Otherwise debugging would be hell. -Steve
Not asking the compiler to fix my errors. When would is(this, myClass) not mean: is(typeof(this) : typeof(myClass))? Why would "is(this, myClass)" be ambiguous? What other interpretation would "is(this, myClass)" imply? Patrick
Oct 20 2017
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/20/17 6:23 PM, Patrick wrote:
 On Friday, 20 October 2017 at 22:15:36 UTC, Steven Schveighoffer wrote:
 On 10/20/17 5:55 PM, Patrick wrote:
 Due to the very specific nature of the 'is' operator, why wouldn't 
 the compiler know to implicitly query the class types? Why must it be 
 explicitly written, typeof(this)?
The compiler generally doesn't "fix" errors for you, it tells you there is a problem, and then you have to fix it. You have to be clear and unambiguous to the compiler. Otherwise debugging would be hell.
Not asking the compiler to fix my errors. When would is(this, myClass) not mean: is(typeof(this) : typeof(myClass))?
class C { } int c; C myC; is(myC : c); oops, forgot to capitalize. But compiler says "I know, you really meant is(typeof(myC) : typeof(c)) -> false. -Steve
Oct 20 2017
parent reply Patrick <tengai650 gmail.com> writes:
On Friday, 20 October 2017 at 23:01:25 UTC, Steven Schveighoffer 
wrote:
 On 10/20/17 6:23 PM, Patrick wrote:
 On Friday, 20 October 2017 at 22:15:36 UTC, Steven 
 Schveighoffer wrote:
 On 10/20/17 5:55 PM, Patrick wrote:
 Due to the very specific nature of the 'is' operator, why 
 wouldn't the compiler know to implicitly query the class 
 types? Why must it be explicitly written, typeof(this)?
The compiler generally doesn't "fix" errors for you, it tells you there is a problem, and then you have to fix it. You have to be clear and unambiguous to the compiler. Otherwise debugging would be hell.
Not asking the compiler to fix my errors. When would is(this, myClass) not mean: is(typeof(this) : typeof(myClass))?
class C { } int c; C myC; is(myC : c); oops, forgot to capitalize. But compiler says "I know, you really meant is(typeof(myC) : typeof(c)) -> false. -Steve
If I explicitly wrote: is(typeof(myC) : typeof(c)) the outcome would still be false and it would still require debugging. So your example demonstrates nothing other then a type-o was made. Try again... In this unique case, the compiler should identify the class and primitive types are incompatible and should issue an error instead (and not return false). Patrick
Oct 20 2017
parent reply Igor <stojkovic.igor gmail.com> writes:
On Friday, 20 October 2017 at 23:24:17 UTC, Patrick wrote:
 On Friday, 20 October 2017 at 23:01:25 UTC, Steven 
 Schveighoffer wrote:
 On 10/20/17 6:23 PM, Patrick wrote:
 On Friday, 20 October 2017 at 22:15:36 UTC, Steven 
 Schveighoffer wrote:
 On 10/20/17 5:55 PM, Patrick wrote:
 Due to the very specific nature of the 'is' operator, why 
 wouldn't the compiler know to implicitly query the class 
 types? Why must it be explicitly written, typeof(this)?
The compiler generally doesn't "fix" errors for you, it tells you there is a problem, and then you have to fix it. You have to be clear and unambiguous to the compiler. Otherwise debugging would be hell.
Not asking the compiler to fix my errors. When would is(this, myClass) not mean: is(typeof(this) : typeof(myClass))?
class C { } int c; C myC; is(myC : c); oops, forgot to capitalize. But compiler says "I know, you really meant is(typeof(myC) : typeof(c)) -> false. -Steve
If I explicitly wrote: is(typeof(myC) : typeof(c)) the outcome would still be false and it would still require debugging. So your example demonstrates nothing other then a type-o was made. Try again... In this unique case, the compiler should identify the class and primitive types are incompatible and should issue an error instead (and not return false). Patrick
But with the current compiler you would never write is(typeOf(myC) : typeof(c)) if in your mind "c" is actually a class "C" because if that is in your mind you would just write is(typeof(myC) : c) which would get you the error. You only need typeof(variable) to get to the type, there is no point in doing typeof(type), you just write type and C is a type. Right?
Oct 21 2017
parent Patrick <tengai650 gmail.com> writes:
 But with the current compiler you would never write

 is(typeOf(myC) : typeof(c))

 if in your mind "c" is actually a class "C" because if that is 
 in your mind you would just write

 is(typeof(myC) : c)

 which would get you the error. You only need typeof(variable) 
 to get to the type, there is no point in doing typeof(type), 
 you just write type and C is a type. Right?
But what value is evaluating a class type to a primitive type? It will never be true? There has to be a reasonable chance for the evaulation to be true for the 'is' operator to provide value. The compiler should reject this 'is' statement or at minimum issue a warning of incompatible types. This is nonsense creep of unusable code (being able to write code that has no meaning and value). This is equivalent to writing code like the following. It is just more obfuscated: if(false) { ... } else { ... } Patrick
Oct 21 2017
prev sibling parent reply user1234 <user1234 12.nl> writes:
On Friday, 20 October 2017 at 21:42:32 UTC, Jonathan M Davis 
wrote:
 On Friday, October 20, 2017 21:32:48 Patrick via 
 Digitalmars-d-learn wrote:
 The compiler seems to reject the following code in a class 
 method:

 bool test = is(this : myClass);

 Could some please explain this?
"this" is not a type.
Strangely this is not always true, in other contexts this is seen as atype, although probably a bug class Foo { class Bar : this {} static assert(is(Bar : Foo)); }
Oct 20 2017
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/20/17 7:04 PM, user1234 wrote:
 Strangely this is not always true, in other contexts this is seen as 
 atype, although probably a bug
 
 class Foo
 {
      class Bar : this {}
      static assert(is(Bar : Foo));
 }
 
Definitely a bug. You should have to write typeof(this) (which is valid in this context). -Steve
Oct 20 2017