Thread overview
weak class conversions are LEGAL?
Jul 05, 2004
Mike Parker
Jul 05, 2004
Andy Friesen
Jul 05, 2004
Norbert Nemec
Jul 05, 2004
Arcane Jill
Jul 05, 2004
Walter
Jul 05, 2004
Daniel Horn
Jul 05, 2004
Walter
July 05, 2004
i'm liking D, but i came across this .... ugliness.

class A
{
 int x;
}

class B
{
 int y;
}

int main()
{
 A a=new A;
 B b=cast(B) a;
 return 0;
}

trying to access members of b will cause a memory access violation.

why am i allowed to do this?


July 05, 2004
Jarrett Billingsley wrote:
> i'm liking D, but i came across this .... ugliness.
> 
> class A
> {
>  int x;
> }
> 
> class B
> {
>  int y;
> }
> 
> int main()
> {
>  A a=new A;
>  B b=cast(B) a;
>  return 0;
> }
> 
> trying to access members of b will cause a memory access violation.
> 
> why am i allowed to do this?
> 
> 
When a cast fails, the result is null. So in this case you can catch the error with a simple test:

if(b === null)
	printf("B is null");

or alternatively use the 'is' operator, which is the same as '===':

if(b is null)
 ...
July 05, 2004
Mike Parker wrote:
> When a cast fails, the result is null. So in this case you can catch the error with a simple test:
> 
> if(b === null)
>     printf("B is null");
> 
> or alternatively use the 'is' operator, which is the same as '===':
> 
> if(b is null)
>  ...

Right, but it's pretty strange that it's not a compile-time error to cast an A to a B, even though B does not inherit A.  The cast *has* to fail, so why is it even allowed?

 -- andy
July 05, 2004
Andy Friesen wrote:

> Mike Parker wrote:
>> When a cast fails, the result is null. So in this case you can catch the error with a simple test:
>> 
>> if(b === null)
>>     printf("B is null");
>> 
>> or alternatively use the 'is' operator, which is the same as '===':
>> 
>> if(b is null)
>>  ...
> 
> Right, but it's pretty strange that it's not a compile-time error to cast an A to a B, even though B does not inherit A.  The cast *has* to fail, so why is it even allowed?

True. The specifications are not very specific in this point (Heyheyhey,
what a pun!!!):

"D differs from C/C++ in another aspect of casts. Any casting of a class reference to a derived class reference is done with a runtime check to make sure it really is a proper downcast. This means that it is equivalent to the behavior of the dynamic_cast operator in C++."

A sentence should be added:

"Casts between references to unrelated classes references are illegal."

July 05, 2004
In article <ccalsv$c9v$1@digitaldaemon.com>, Mike Parker says...

>When a cast fails, the result is null. So in this case you can catch the error with a simple test:

But why should we have to add run-time code for something that could be detected at compile-time? This is a compile-time error. It should be reported at compile-time.

Arcane Jill


July 05, 2004
"Arcane Jill" <Arcane_member@pathlink.com> wrote in message news:ccb2t5$10tm$1@digitaldaemon.com...
> In article <ccalsv$c9v$1@digitaldaemon.com>, Mike Parker says...
>
> >When a cast fails, the result is null. So in this case you can catch the error with a simple test:
>
> But why should we have to add run-time code for something that could be
detected
> at compile-time? This is a compile-time error. It should be reported at compile-time.

It's a runtime error because one may be asking this question as part of the logic of the implementation of a template that selects one of several alternatives based on the type of a parameter.


July 05, 2004
Walter wrote:
> "Arcane Jill" <Arcane_member@pathlink.com> wrote in message
> news:ccb2t5$10tm$1@digitaldaemon.com...
> 
>>In article <ccalsv$c9v$1@digitaldaemon.com>, Mike Parker says...
>>
>>
>>>When a cast fails, the result is null. So in this case you can catch the
>>>error with a simple test:
>>
>>But why should we have to add run-time code for something that could be
> 
> detected
> 
>>at compile-time? This is a compile-time error. It should be reported at
>>compile-time.
> 
> 
> It's a runtime error because one may be asking this question as part of the
> logic of the implementation of a template that selects one of several
> alternatives based on the type of a parameter.
> 
> 
You're supposed to do that through template specialization, not run time checks :-), no?
July 05, 2004
"Daniel Horn" <hellcatv@hotmail.com> wrote in message news:ccc6jd$2t30$1@digitaldaemon.com...
> Walter wrote:
> > It's a runtime error because one may be asking this question as part of
the
> > logic of the implementation of a template that selects one of several alternatives based on the type of a parameter.
> You're supposed to do that through template specialization, not run time checks :-), no?

Trying to do everything with specialization has been listed as one of the problems with C++ generic programming by some people who have made their reputations writing template libraries, so I am inclined to believe them <g>.