-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typed Functions with signatures #207
Comments
This is definitely something I have always wanted to see in AS3. If this were to be implemented, one thing that should be allowed is something like this: var fun:Function.<(ev:Event):void> = function(ev:Object):void {}; To be clear, var obj:Object = new Event(); With return types, there's a similar situation, but instead of superclasses, subclasses would be allowed.
|
@joshtynjala would that not be?:
|
No. I think the point was that you should be able to override the type restrictions by explicitly using Object. That's a general AS3 feature available elsewhere. |
yes, but this: |
OK. I see what you mean. I'll leave it to Josh to answer for himself... 😉 |
As I was writing that, I wondered if it would be clear enough using Let me fix that. This should be allowed: var fun:Function.<(ev:MouseEvent):void> = function(ev:Event):void {}; I don't have the types backwards. Consider the following, which is allowed in AS3 code that exists today:
This is why Function types in Haxe and TypeScript both work how I describe. Here's a TypeScript example: let myListener1: (event: MouseEvent)=>void = function(event: Event) {} // no problem!
let myListener2: (event: Event)=>void = function(event: MouseEvent) {} // error! |
Thanks for the explanation, @joshtynjala, it's been a while since I used this stuff in Haxe, and I didn't use TypeScript yet. I am used the declaration/assignment pattern of And it seems inverted here when looking at it (without thinking too deeply about it I guess). So probably I am also getting confused about runtime vs. compiletime behavior here. for
|
Yeah, it's kind of tricky. With function types, you need to think about what arguments get passed in when the function is called to understand what other function type can be assigned. The other way around wouldn't work: var fun:Function.<(ev:Event):void> = function(ev:MouseEvent):void {}; // this should error, but what if not?
fun(new FocusEvent()); // whoa, that's not a MouseEvent! |
An aside: Why does Vector have the syntax |
You'd need to ask someone from Adobe about the full details, but I recall that there was some concern about whether |
I guess we should stick to the TypeScript form maybe? ES4 was fine too, but I am confused as to what is best. I have supported type F0 = () => void;
type F1 = (...a: Array) => void; Also, ABC does not support function types, so the compiler should erase them at the output ABC in the meanwhile. |
Just to update, I've updated AS3Parser to comply with ECMAScript 4 syntax for function type expressions.
|
Currently in ActionScript, it's only possible to declare function signatures for methods and package level functions. It's not possible to declare signatures for variables and function paramters. The best you have is
var foo:Function
orfunction foo(fun:Function):Function{}
.This in my opinion is the biggest hole in the ActionScript type system. I have felt this lack for a very long time. It's very commonly felt in event handlers, but possibly a bigger issue is that it makes ActionScript a poor choice for functional-style programming.
If I had only one feature I could add to the language, it would be typed functions with signatures.
The exact syntax is less important than the type safety. My initial suggestion would be Vector style angle brackets with content like you'd use for an interface. Something like this:
var fun:Function.<(ev:Event):void>
orfunction foo(callback:Function.<(handler:IAsyncTask):IAsyncTask>):void{}
.The text was updated successfully, but these errors were encountered: