Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 20, 2017 is(this : myClass) | ||||
---|---|---|---|---|
| ||||
The compiler seems to reject the following code in a class method: bool test = is(this : myClass); Could some please explain this? Thanks, Patrick |
October 20, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick | 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
|
October 20, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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
|
October 20, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick | 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
|
October 20, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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
|
October 20, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick | 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
|
October 20, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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));
}
|
October 20, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to user1234 | 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
|
October 20, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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
|
October 21, 2017 Re: is(this : myClass) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick | 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?
|
Copyright © 1999-2021 by the D Language Foundation