digitalmars.D - weak class conversions are LEGAL?
- Jarrett Billingsley (17/17) Jul 04 2004 i'm liking D, but i came across this .... ugliness.
- Mike Parker (8/32) Jul 04 2004 When a cast fails, the result is null. So in this case you can catch the...
- Andy Friesen (5/15) Jul 04 2004 Right, but it's pretty strange that it's not a compile-time error to
- Norbert Nemec (9/24) Jul 05 2004 True. The specifications are not very specific in this point (Heyheyhey,
- Arcane Jill (5/7) Jul 05 2004 But why should we have to add run-time code for something that could be ...
- Walter (6/12) Jul 05 2004 detected
- Daniel Horn (3/25) Jul 05 2004 You're supposed to do that through template specialization, not run time...
- Walter (7/13) Jul 05 2004 the
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?
Jul 04 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) ...
Jul 04 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
Jul 04 2004
Andy Friesen wrote:Mike Parker wrote: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."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?
Jul 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
Jul 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...detectedWhen 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 beat 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.
Jul 05 2004
Walter wrote:"Arcane Jill" <Arcane_member pathlink.com> wrote in message news:ccb2t5$10tm$1 digitaldaemon.com...You're supposed to do that through template specialization, not run time checks :-), no?In article <ccalsv$c9v$1 digitaldaemon.com>, Mike Parker says...detectedWhen 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 beat 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.
Jul 05 2004
"Daniel Horn" <hellcatv hotmail.com> wrote in message news:ccc6jd$2t30$1 digitaldaemon.com...Walter wrote:theIt's a runtime error because one may be asking this question as part ofTrying 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>.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?
Jul 05 2004