#814 Invalid casting in JavaScript

Yuri Strot Fri 13 Nov 2009

Hi folks,

I've caught interesting problem with JavaScript. Example is following:

@js
const class A
{
  const Float a1
  const Float a2

  new make(Float a1, Float a2) { this.a1 = a1; this.a2 = a2 }

  Float sum() { a1 + a2 }

  B toB() { B(sum()) }

}
@js
const class B
{
  const Float? b

  new make(Float? b) { this.b = b }
}

Now when I try a := A(0f, 0f); b := a.toB() I get exception:

Uncaught sys::CastErr: sys::Int cannot be cast to sys::Float?

Actually, when we define a it has m_a1 = 0, m_a2 = 0 and $fanType = sys::Float for both variables. But when we call A.sum() it simply return 0 without any fanType. However, in the A.toB we have following:

fan.sys.Obj.coerce(this.sum(),fan.sys.Type.find("sys::Float?"))

So, I've got this exception because return value of this.sum() interpreted as sys::Int.

Any suggestions?

andy Fri 13 Nov 2009

There likely a number of these edge cases lurking around. I'm slowly going back and reworking compilerJs and the sys library to get the core runtime solid.

If you want to work around bugs like that though, you can disable type coercion:

// fan/src/sys/js/fan/Obj.js:172
fan.sys.Obj.coerce = function(obj, type)
{
  /* comment out existing code */
  return obj;
}

Yuri Strot Fri 13 Nov 2009

Okay, this works for me.

Good luck with your job, Andy! The world will be much happier when web applications can be written on awesome Fan! :-)

Yuri Strot Fri 13 Nov 2009

...I mean Fantom. The new name is really cool, just need some practice.

Login or Signup to reply.