Skip to content

Instantly share code, notes, and snippets.

@nadako
Last active December 19, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadako/5957084 to your computer and use it in GitHub Desktop.
Save nadako/5957084 to your computer and use it in GitHub Desktop.
Type inference fail.
-cp src
-main Main
-swf bin/Main.swf
-cmd start bin/Main.swf
package;
class Main
{
public function new(root)
{
root.addEventListener(flash.events.Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(_)
{
}
static function main()
{
new Main(flash.Lib.current);
}
}
src/Main.hx:16: characters 11-28 : flash.display.MovieClip should be { addEventListener : String -> (_ : Unknown<1> -> Void) -> Unknown<0> }
src/Main.hx:16: characters 11-28 : Invalid type for field addEventListener :
src/Main.hx:16: characters 11-28 : type : String -> listener : (Dynamic -> Void) -> ?useCapture : Bool -> ?priority : Int -> ?useWeakReference : Bool -> Void should be String -> (_ : Unknown<1> -> Void) -> Unknown<0>
src/Main.hx:16: characters 11-28 : For function argument 'root'
@nadako
Copy link
Author

nadako commented Jul 9, 2013

It works well if I specify a type for the "root" argument of the "new" function explicitly. I wonder if it's because optional arguments or I just don't understand something.

@nadako
Copy link
Author

nadako commented Jul 9, 2013

[15:00:59] <Simn> It's the optional arguments.
[15:02:39] <nadako> hm, yeah it works if I specify values for optional arguments in the addEventListener call without specifying type
[15:03:58] <Simn> yea, there's a reason for that
[15:04:14] <nadako> Though I wonder why it's not possible to match the signature that way. Does it create any ambiguity? Sorry I'm kinda new to the whole type inference thing :)
[15:04:57] <Simn> It has to do with how code is generated. Some platforms require optional arguments to be padded with null, which only works if the compile-time knows about them.
[15:05:33] <Simn> You cannot call a foo(?opt) function as foo() on all targets, so the compiler has to deal with that in some way.
[15:06:07] <nadako> Hm, that makes sense. Thanks! Though maybe then compiler could give more meaningful error message :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment