Type-inference gotcha

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Type-inference gotcha

Bill Moorier
I was just tripped up by haXe's type-inference, unfortunately to the
extent that some buggy code went into production.  I don't think haXe
does the right thing here, to be honest.  Here's what happened:

I have a function:

    public static function getSwfArg(name, defaultValue=null) {...}

That was being used elsewhere:

    if(getSwfArg("onsite", false)) {...}

Apparently, without type declarations, haXe assumes the defaultValue
argument is a String.  Some nasty type-coercion happens, with the
result that the call to getSwfArg will never return false (it returns
the String "false" instead).

This is fixed by changing the function definition to:

    public static function getSwfArg(name, defaultValue : Dynamic =
null) : Dynamic {...}

But it seems very counter-intuitive that I would need that declaration
to make the program work.

Cheers,
Bill.
--
Bill Moorier: http://abstractnonsense.com
VP Software Development, http://www.justin.tv

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Type-inference gotcha

Nicolas Cannasse
Bill Moorier a écrit :

> I was just tripped up by haXe's type-inference, unfortunately to the
> extent that some buggy code went into production.  I don't think haXe
> does the right thing here, to be honest.  Here's what happened:
>
> I have a function:
>
>     public static function getSwfArg(name, defaultValue=null) {...}
>
> That was being used elsewhere:
>
>     if(getSwfArg("onsite", false)) {...}
>
> Apparently, without type declarations, haXe assumes the defaultValue
> argument is a String.  Some nasty type-coercion happens, with the
> result that the call to getSwfArg will never return false (it returns
> the String "false" instead).
>
> This is fixed by changing the function definition to:
>
>     public static function getSwfArg(name, defaultValue : Dynamic =
> null) : Dynamic {...}
>
> But it seems very counter-intuitive that I would need that declaration
> to make the program work.

Yes, in Flash9 you have to be careful to specify which values are
Dynamic, because the runtime will easily do coercions without you being
aware. I would have prefer that in theses cases the AVM2 would report an
exception, but that's sadly not the case.

Type inference is still very useful because in general you use more
typed variables than Dynamic ones ;)

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Type-inference gotcha

Jay Cain
I love type inference, especially for local variables because it simplifies the code so much. However, It would be nice to have a compiler option to check for explicit return type declarations on functions. It could issue either a warning or an error, depending on the option, perhaps. I sometimes forget the return type I'm should expect and sifting through a function to figure it out can take a little time (especially since the variable types are inferred). I try to maintain a policy of explicitly declaring return type, but sometimes I forget that, too. Maybe I'm just forgetful...
 
~Jay

On Sun, Nov 23, 2008 at 4:22 AM, Nicolas Cannasse <[hidden email]> wrote:
Bill Moorier a écrit :

I was just tripped up by haXe's type-inference, unfortunately to the
extent that some buggy code went into production.  I don't think haXe
does the right thing here, to be honest.  Here's what happened:

I have a function:

   public static function getSwfArg(name, defaultValue=null) {...}

That was being used elsewhere:

   if(getSwfArg("onsite", false)) {...}

Apparently, without type declarations, haXe assumes the defaultValue
argument is a String.  Some nasty type-coercion happens, with the
result that the call to getSwfArg will never return false (it returns
the String "false" instead).

This is fixed by changing the function definition to:

   public static function getSwfArg(name, defaultValue : Dynamic =
null) : Dynamic {...}

But it seems very counter-intuitive that I would need that declaration
to make the program work.

Yes, in Flash9 you have to be careful to specify which values are Dynamic, because the runtime will easily do coercions without you being aware. I would have prefer that in theses cases the AVM2 would report an exception, but that's sadly not the case.

Type inference is still very useful because in general you use more typed variables than Dynamic ones ;)

Best,
Nicolas


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org