It's a spec thing, and the compiler is doing the right thing:
The normal casting takes precedence over user-defined casting.

Bard, please update the spec and specify priorities for casting:
- if a regular (java-like) casting can be done, we do that.
- otherwise we try user-defined casts


for example:

public class Hello
{
   public static def main(Array[String])
   {
       val b1:B = new B();
       Console.OUT.println("b1 as C: " + b1 as C); // Must use the
user-defined casting, so we get: b1 as C: C@a32b
       val b2:Base = b1;
       Console.OUT.println("b2 as C: " + b2 as C); // Can use the regular
casting, so we get: x10.lang.ClassCastException: C
   }
}

protected class Base
{
   public static operator (base: Base) as C = new C();
}
protected class B extends Base {}
protected class C extends Base {}





PS
Your examples always gives me the same output:
$ x10 Hello
b: 15
b as C: 42
c: 51
c as C: 51

b2: 15
b2 as C: 42
c2: 51
c2 as C: 51

b3: 15
x10.lang.ClassCastException: C
        at x10.rtt.Types.cast(Types.java:338)
        at Hello.main(Hello.java:117)
        at Hello$$Main.runtimeCallback(Hello.java:26)
        at x10.runtime.impl.java.Runtime$3.$apply(Runtime.java:109)
        at x10.lang.Runtime$$Closure$144.$apply(Runtime.java:4138)
        at x10.lang.Activity.run(Activity.java:594)
        at x10.lang.Runtime$Worker$$Closure$137.$apply(Runtime.java:1347)
        at x10.lang.Runtime.runAtLocal(Runtime.java:215)
        at x10.lang.Runtime$Worker.loop(Runtime.java:1055)
        at x10.lang.Runtime$Worker.$apply(Runtime.java:988)
        at x10.lang.Runtime$Pool.$apply(Runtime.java:1659)
        at x10.lang.Runtime.start(Runtime.java:2368)
        at x10.runtime.impl.java.Runtime.$apply(Runtime.java:87)
        at x10.runtime.impl.java.Thread.run(Thread.java:48)


2011/2/9 Kröhnert, Manfred <manfred.kroehn...@kit.edu>

> Hi, see below for the test program.
>
>
> Am 08.02.2011 um 21:01 schrieb Igor Peshansky:
>
> > Kröhnert, Manfred <manfred.kroehn...@kit.edu> wrote on 02/08/2011
> 02:49:01
> > PM:
> >
> >> Hello everyone,
> >>
> >> I have a problem with the attached test program.
> >>
> >> It declares a class 'Base' and two other classes 'B' and 'C' which
> >> extend 'Base'.
> >> On each class a method 'asC(): C' is implemented which returns an
> >> instance of Class 'C'.
> >> The class 'Base' contains a static cast operator which invokes 'asC()'
> >> on the given object.
> >>
> >> Afterwards instances of 'B' and 'C' are created in two different ways
> >> and the objects are then cast to type 'C'  by invoking the 'as C'
> >> operator and by calling the 'asC()' method.
> >>
> >> I would expect the program to behave the same in all occasions but it
> >> aborts with a ClassCastException and produces the following output:
> >>
> >> b: 15
> >> b as C: 42
> >> Uncaught exception at place 0: x10.lang.MultipleExceptions: null
> >> c: 51
> >> x10.lang.ClassCastException: null
> >> c as C: 51
> >>
> >> b2: 15
> >> b2 as C: 42
> >> c2: 51
> >> c2 as C: 51
> >>
> >> b3: 15
> >>   at x10::lang::Throwable::fillInStackTrace()
> >>   at x10aux::throwClassCastException()
> >>   at x10aux::ref<C> x10aux::real_class_cast<C>(x10aux::ref<x10::
> >> lang::Reference>, bool)
> >>   at x10aux::ClassCastNotPrimitive<x10aux::ref<C>, x10aux::ref<Base>
> >>> ::_(x10aux::ref<Base>, bool)
> >>   at x10aux::ClassCastPrimitive<x10aux::ref<C>, x10aux::ref<Base> >::
> >> _(x10aux::ref<Base>, bool)
> >>   at x10aux::ClassCast<x10aux::ref<C>, x10aux::ref<Base> >::
> >> _(x10aux::ref<Base>, bool)
> >>   at x10aux::ref<C> x10aux::class_cast<x10aux::ref<C>, x10aux::
> >> ref<Base> >(x10aux::ref<Base>)
> >>   at Hello::main(x10aux::ref<x10::array::Array<x10aux::ref<x10::
> >> lang::String> > >)
> >>   at x10aux::BootStrapClosure::apply()
> >>   at x10_lang_Runtime__closure__2::apply()
> >>   at x10::lang::Activity::run()
> >>   at x10::lang::Runtime__Worker::loop(x10aux::ref<x10::lang::
> >> SimpleLatch>, bool)
> >>   at x10::lang::Runtime__Worker::apply()
> >>   at x10::lang::Runtime__Pool::apply()
> >>   at x10::lang::Runtime::start(x10aux::ref<x10::lang::VoidFun_0_0>,
> >> x10aux::ref<x10::lang::VoidFun_0_0>)
> >>   at int x10aux::template_main<x10::lang::Runtime, Hello>(int, char**)
> >>   at main
> >>   at start
> >>
> >> Is this a bug in the compiler or in the runtime or is it expected
> > behaviour?
> >> I am using the C++ backend with X10DT 2.1.1 on OS X 10.6.6 (Intel).
> >
> > Manfred,
> >
> > This list strips attachments.  If the program is small enough, please
> > include it verbatim.  Otherwise, please either open a JIRA (if you
> believe
> > that this disagrees with the language specification) or try to reduce
> this
> > to a small test case that can be posted verbatim.
> >
> > Thanks,
> >        Igor
>
>
> Thanks for the hint Igor. I didn't know about the list removing
> attachments.
> Below is the contents of the file.
>
> If this sample is too big to post on the list let me know and I'll open a
> JIRA next time.
>
> Best regards,
>
> Manfred
>
>
> ------------------------- Test.x10 --------------------------
>
> public class Hello
> {
>    public static def main(Array[String])
>    {
>        val b = new B(15);
>        val c = new C(51);
>        Console.OUT.println("b: " + b.value);
>        Console.OUT.println("b as C: " + (b as C).value);
>        Console.OUT.println("c: " + c.value);
>        Console.OUT.println("c as C: " + (c as C).value);
>        val b2 = get(15);
>        val c2 = get(51);
>        Console.OUT.println("\nb2: " + b2.value);
>        Console.OUT.println("b2 as C: " + (b2.asC()).value);
>        Console.OUT.println("c2: " + c2.value);
>        Console.OUT.println("c2 as C: " + (c2.asC()).value);
>        val b3 = get(15);
>        val c3 = get(51);
>        Console.OUT.println("\nb3: " + b.value);
>        Console.OUT.println("b3 as C: " + (b3 as C).value);
>        Console.OUT.println("c3: " + c3.value);
>        Console.OUT.println("c3 as C: " + (c3 as C).value);
>    }
>
>    public static def get(i: Int) : Base
>    {
>        if (i < 50)
>            return new B(i);
>        else
>            return new C(i);
>    }
> }
>
> protected class Base
> {
>    public val value: Int;
>    public def this(value: Int) {
>        this.value = value;
>    }
>    public static operator (base: Base) as C = base.asC();
>    public def asC(): C = new C(24);
> }
>
>
> protected class B extends Base {
>    public def this(value: Int) {
>        super(value);
>    }
>    public def asC(): C = new C(42);
> };
>
>
> protected class C extends Base {
>    public def this(value: Int) {
>        super(value);
>    }
>    public def asC(): C = this;
> };
>
> ------------------------- Test.x10 --------------------------
>
>
>
> ------------------------------------------------------------------------------
> 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
>
------------------------------------------------------------------------------
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