#1560 Constructors and Ternary operators

kaushik Tue 14 Jun 2011

The following code throws ClassCastException at runtime, should instead be a compiler error

class HelloFantom{
  Void main(){
    HelloFantom h := false?HelloFantom():"test"
  }
}

brian Tue 14 Jun 2011

Although not ideal, that is technically the correct behavior from an expression point of view.

The expression false?HelloFantom():"test" has an inferred typed of Obj since that is the common base type between HelloFantom and Str. Then the entire expression is coerced from Obj to HelloFantom using Fantom's normal rules.

Login or Signup to reply.