You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@royale.apache.org by GitBox <gi...@apache.org> on 2022/01/18 08:31:47 UTC
[GitHub] [royale-compiler] Harbs opened a new issue #207: Typed Functions with signatures
Harbs opened a new issue #207:
URL: https://github.com/apache/royale-compiler/issues/207
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` or `function 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>` or `function foo(callback:Function.<(handler:IAsyncTask):IAsyncTask>):void{}`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] joshtynjala commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
joshtynjala commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1016677175
You'd need to ask someone from Adobe about the full details, but I recall that there was some concern about whether `Vector<Foo>` might have some kind of potential syntax ambiguity, and they felt that it was safer to use `Vector.<Foo>` instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] joshtynjala commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
joshtynjala commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015864334
As I was writing that, I wondered if it would be clear enough using `Object`, or if I should use more specific classes. I guess I should have been more specific after all.
Let me fix that. This should be allowed:
```as3
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:
```
function myListener(event:Event):void {}
myListener(new MouseEvent());
``
This is why `myListener` should be allowed to be assigned to `var fun:Function.<(ev:MouseEvent):void>`. Because the type is `Event`, it can accept any subclass, including `MouseEvent`..
Function types in Haxe and TypeScript both work how I describe. Here's a TypeScript example:
```ts
let myListener1: (event: MouseEvent)=>void = function(event: Event) {} // no problem!
let myListener2: (event: Event)=>void = function(event: MouseEvent) {} // error!
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] joshtynjala edited a comment on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
joshtynjala edited a comment on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015823549
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:
```as3
var fun:Function.<(ev:Event):void> = function(ev:Object):void {};
```
To be clear, `Event` is a subclass of `Object`, so that function should be allowed for the same reason why you can assign an `Event` instance to a variable of type `Object`.
```as3
var obj:Object = new Event();
```
With return types, there's a similar situation, but instead of superclasses, subclasses would be allowed.
```
var fun:Function.<():Object> = function():Event {}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] Harbs commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
Harbs commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015853693
OK. I see what you mean. I'll leave it to Josh to answer for himself... 😉
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] joshtynjala edited a comment on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
joshtynjala edited a comment on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015864334
As I was writing that, I wondered if it would be clear enough using `Object`, or if I should use more specific classes. I guess I should have been more specific after all.
Let me fix that. This should be allowed:
```as3
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:
```
function myListener(event:Event):void {}
myListener(new MouseEvent());
```
This is why `myListener` should be allowed to be assigned to `var fun:Function.<(ev:MouseEvent):void>`. Because the type is `Event`, it can accept any subclass, including `MouseEvent`..
Function types in Haxe and TypeScript both work how I describe. Here's a TypeScript example:
```ts
let myListener1: (event: MouseEvent)=>void = function(event: Event) {} // no problem!
let myListener2: (event: Event)=>void = function(event: MouseEvent) {} // error!
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] Harbs commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
Harbs commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1016383846
An aside: Why does Vector have the syntax `Vector.<Foo>` instead of `Vector<Foo>`? I've always found the extra dot weird and hard to remember...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] joshtynjala commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
joshtynjala commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015900829
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:
```as3
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!
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] greg-dove commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
greg-dove commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015893391
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
var name:Type = value of Type or SubType
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
`var fun:Function.<(ev:MouseEvent):void> = function(ev:Event):void {};`
`fun(new Event('blah'))` will fail at compile time, which is the main point, I guess.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] greg-dove commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
greg-dove commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015852290
> 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: `var fun:Function.<(ev:Event):void>` is strongly typed to Event as a parameter, not Object. So it should accept function values with ev:EventSubclass, but not Object. That is generally how these things work, I think.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] greg-dove commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
greg-dove commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015847111
> ```actionscript-3
> var fun:Function.<(ev:Event):void> = function(ev:Object):void {};
> ```
@joshtynjala would that not be?:
```actionscript-3
var fun:Function.<(ev:Object):void> = function(ev:Event):void {};
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] Harbs commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
Harbs commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015848905
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [royale-compiler] joshtynjala commented on issue #207: Typed Functions with signatures
Posted by GitBox <gi...@apache.org>.
joshtynjala commented on issue #207:
URL: https://github.com/apache/royale-compiler/issues/207#issuecomment-1015823549
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:
```as3
var fun:Function.<(ev:Event):void> = function(ev:Object):void {};
```
To be clear, Event is a subclass of Object, so that function should be allowed for the same reason why you can assign an `Event` instance to a variable of type `Object`.
```as3
var obj:Object = new Event();
```
With return types, there's a similar situation, but instead of superclasses, subclasses would be allowed.
```
var fun:Function.<():Object> = function():Event {}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@royale.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org