Hello Yoav,

Am 09.02.2011 um 18:19 schrieb Yoav Zibin:

> I think you didn't understand me.
> When you have:
> class Base {}
> class C extends Base {}
> and you write:
> val b:Base = ...;
> val c:C = b as C; // this does normal casting (like in Java)
> 
> That's what your code does (the compiler prefers the normal casting ala Java
> over the user-defined casting),
> and if the dynamic type of "b" is not C, then it will throw
> ClassCastException (which it does).
> There is no bug in the compiler or runtime, but the behavior is tricky and
> not trivial to understand.

thanks for the more elaborate explanation.
Now it is clear to me why the exception gets thrown.

I guess my main confusion arises from the fact that the user defined cast is 
not preferred even when it is implemented on the base class and therefore 
available at compile time.

> I actually have an easier solution for you:
> Don't use user-defined "as" operator.
> They don't exists in Java, and I personally believe that they only cause
> confusion.
> You can just define your own static/instance methods that knows how to
> convert a base object to type C.

Yes, I am currently using instance methods which get overwritten by subclasses.
So I will just not use the cast operator for now.

Thanks and best regards,

Manfred


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users

Reply via email to