indeterminate amount of parameters

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

indeterminate amount of parameters

Nathan
Hi All,

Wondering if there's a way to have an indeterminate amount of parameters
on a function/method in haXe. Essentially I want to have 1...* typed
params on a function

Class Foo {
   public function write( text:String... ) : Void {}

Where you could pass in any number of String's the method and it would
be happy (but would fail with a diff type)

   var o:Foo = new Foo();
   o.write( "hello", "there", "haxe" );

Possible?

Regards,

Nathan

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

Re: indeterminate amount of parameters

Nathan
Nathan wrote:

> Hi All,
>
> Wondering if there's a way to have an indeterminate amount of parameters
> on a function/method in haXe. Essentially I want to have 1...* typed
> params on a function
>
> Class Foo {
>   public function write( text:String... ) : Void {}
>
> Where you could pass in any number of String's the method and it would
> be happy (but would fail with a diff type)
>
>   var o:Foo = new Foo();
>   o.write( "hello", "there", "haxe" );
>
> Possible?

I've just been pointed to makeVarArgs in the reflection api (by
jan_flanders in irc#haxe), which is great - the caveat is that I should
have mentioned this is for an extern class/interface that I'm not
implementing directly. Thus afaict, the reflection makeVarArgs method
won't work and I'm needing some syntax-sugar.

Cheers,

Nathan



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

Re: indeterminate amount of parameters

Justin Donaldson-2
yes, I have this problem too occasionally.  Typically haxe externs will include explicit argument definitions, but only for the first few optional arguments (usually up to 6).  The only other way (afaik) is to type the extern function as Dynamic.

-Justin


On Sun, Aug 15, 2010 at 5:06 PM, Nathan <[hidden email]> wrote:
Nathan wrote:
Hi All,

Wondering if there's a way to have an indeterminate amount of parameters on a function/method in haXe. Essentially I want to have 1...* typed params on a function

Class Foo {
 public function write( text:String... ) : Void {}

Where you could pass in any number of String's the method and it would be happy (but would fail with a diff type)

 var o:Foo = new Foo();
 o.write( "hello", "there", "haxe" );

Possible?

I've just been pointed to makeVarArgs in the reflection api (by jan_flanders in irc#haxe), which is great - the caveat is that I should have mentioned this is for an extern class/interface that I'm not implementing directly. Thus afaict, the reflection makeVarArgs method won't work and I'm needing some syntax-sugar.

Cheers,


Nathan



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



--
blog: http://www.scwn.net
aim: iujjd
twitter: jjdonald


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

Re: indeterminate amount of parameters

Nathan
ahh do you mean:

   public var methodname (default,null) : Dynamic -> Void;
or
   public var methodname : Dynamic;
or..?

cheers Justin,

nathan

Justin Donaldson wrote:

> yes, I have this problem too occasionally.  Typically haxe externs will
> include explicit argument definitions, but only for the first few optional
> arguments (usually up to 6).  The only other way (afaik) is to type the
> extern function as Dynamic.
>
> -Justin
>
>
> On Sun, Aug 15, 2010 at 5:06 PM, Nathan <[hidden email]> wrote:
>
>> Nathan wrote:
>>
>>> Hi All,
>>>
>>> Wondering if there's a way to have an indeterminate amount of parameters
>>> on a function/method in haXe. Essentially I want to have 1...* typed params
>>> on a function
>>>
>>> Class Foo {
>>>  public function write( text:String... ) : Void {}
>>>
>>> Where you could pass in any number of String's the method and it would be
>>> happy (but would fail with a diff type)
>>>
>>>  var o:Foo = new Foo();
>>>  o.write( "hello", "there", "haxe" );
>>>
>>> Possible?
>>>
>> I've just been pointed to makeVarArgs in the reflection api (by
>> jan_flanders in irc#haxe), which is great - the caveat is that I should have
>> mentioned this is for an extern class/interface that I'm not implementing
>> directly. Thus afaict, the reflection makeVarArgs method won't work and I'm
>> needing some syntax-sugar.
>>
>> Cheers,
>>
>>
>> Nathan
>>
>>
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>
>
>


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

Re: indeterminate amount of parameters

Ron Wheeler
In reply to this post by Justin Donaldson-2
Can you not just pass an array of strings as a single argument?

Ron

On 15/08/2010 8:40 PM, Justin Donaldson wrote:
yes, I have this problem too occasionally.  Typically haxe externs will include explicit argument definitions, but only for the first few optional arguments (usually up to 6).  The only other way (afaik) is to type the extern function as Dynamic.

-Justin


On Sun, Aug 15, 2010 at 5:06 PM, Nathan <[hidden email]> wrote:
Nathan wrote:
Hi All,

Wondering if there's a way to have an indeterminate amount of parameters on a function/method in haXe. Essentially I want to have 1...* typed params on a function

Class Foo {
 public function write( text:String... ) : Void {}

Where you could pass in any number of String's the method and it would be happy (but would fail with a diff type)

 var o:Foo = new Foo();
 o.write( "hello", "there", "haxe" );

Possible?

I've just been pointed to makeVarArgs in the reflection api (by jan_flanders in irc#haxe), which is great - the caveat is that I should have mentioned this is for an extern class/interface that I'm not implementing directly. Thus afaict, the reflection makeVarArgs method won't work and I'm needing some syntax-sugar.

Cheers,


Nathan



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



--
blog: http://www.scwn.net
aim: iujjd
twitter: jjdonald



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

Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

singmajesty
In reply to this post by Nathan
Hey guys,


I'm trying to port my first full project over from AS3 to haXe.

So far it's gone alright, but I'm stuck on a couple things. Perhaps  
someone could help?



1.) Function type


As far as I've seen, there's no way to define a generic function type. Is  
that true? It seems like there's a way to add a method as a type, but only  
if you specify the parameters it accepts. If that's true, that really  
seems like a pain. I have some key instances where it helps to support  
generic methods. Here's one example:


KeyBinding.addOnPress (Keyboard.LEFT, myFunction, myParameters);



I have a KeyBinding class, which I use to listen to keys and call methods.  
Would it make sense to strongly type which parameters the target method is  
expecting? Not at all, I think. How do I get around this?




2.) Rest parameters


I also have instances where it's a HUGE help to be able to pass rest  
parameters. I guess this also isn't possible?




3.) Apply


Finally, does something like this work in haXe?


myMethod.apply (null, myParameters);




Thanks for the help!

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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

Lyndon Howie
1)
In this case you can do something like:
KeyBinding.addOnPress (Keyboard.LEFT, function () { myFunction(myParameters); });
for similar effect (so your function type would be Void->Void, but you can basically do whatever you want by just wrapping the function and params you want called by a void->void function).
2) Sorry, I don't understand what a rest parameter is...
3) Sorry, also not quite sure what you mean by that either :)

--- On Mon, 16/8/10, Joshua Granick <[hidden email]> wrote:

> From: Joshua Granick <[hidden email]>
> Subject: [haXe] Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?
> To: "The haXe compiler list" <[hidden email]>
> Received: Monday, 16 August, 2010, 5:19 PM
> Hey guys,
>
>
> I'm trying to port my first full project over from AS3 to
> haXe.
>
> So far it's gone alright, but I'm stuck on a couple things.
> Perhaps someone could help?
>
>
>
> 1.) Function type
>
>
> As far as I've seen, there's no way to define a generic
> function type. Is that true? It seems like there's a way to
> add a method as a type, but only if you specify the
> parameters it accepts. If that's true, that really seems
> like a pain. I have some key instances where it helps to
> support generic methods. Here's one example:
>
>
> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
> myParameters);
>
>
>
> I have a KeyBinding class, which I use to listen to keys
> and call methods. Would it make sense to strongly type which
> parameters the target method is expecting? Not at all, I
> think. How do I get around this?
>
>
>
>
> 2.) Rest parameters
>
>
> I also have instances where it's a HUGE help to be able to
> pass rest parameters. I guess this also isn't possible?
>
>
>
>
> 3.) Apply
>
>
> Finally, does something like this work in haXe?
>
>
> myMethod.apply (null, myParameters);
>
>
>
>
> Thanks for the help!
>
> --haXe - an open source web programming language
> http://haxe.org
>




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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

singmajesty
1.) I tried to do something like that, but it threw an error, saying it  
couldn't access that method in a static function. So far the only way I've  
been able to handle it is to have a class method for every possible  
handler. Sort of a pain, but I guess it works. Hopefully there's a more  
elegant way to handle it?


2.) A "rest" parameter will let you pass an indeterminate number of  
parameters to a method. Here's an example from AS3:



private function myFunction (... parameters:Array) {
       
        for each (var parameter:Object in parameters) {
               
                trace (parameter);
               
        }
       
}


Some of the core methods support "rest" parameters like this, like  
trace(). You can trace ("Hello") or you can trace ("Hello", "World", "!");


3.) Apply will let you take an array of parameters, and pass them into the  
function. Here's an example:



var strings:Array = [ "Hello", "World", "!" ];

trace.apply (null, strings);



It's useful when used in conjuction with rest parameters. You can store an  
unlimited number of parameters to an array, then you can pass those  
parameters to your target method





On Mon, 16 Aug 2010 00:46:35 -0700, Lyndon Howie  
<[hidden email]> wrote:

> 1)
> In this case you can do something like:
> KeyBinding.addOnPress (Keyboard.LEFT, function () {  
> myFunction(myParameters); });
> for similar effect (so your function type would be Void->Void, but you  
> can basically do whatever you want by just wrapping the function and  
> params you want called by a void->void function).
> 2) Sorry, I don't understand what a rest parameter is...
> 3) Sorry, also not quite sure what you mean by that either :)
>
> --- On Mon, 16/8/10, Joshua Granick <[hidden email]> wrote:
>
>> From: Joshua Granick <[hidden email]>
>> Subject: [haXe] Porting from AS3 to haXe: How do I replace a Function  
>> type, rest parameters, and apply?
>> To: "The haXe compiler list" <[hidden email]>
>> Received: Monday, 16 August, 2010, 5:19 PM
>> Hey guys,
>>
>>
>> I'm trying to port my first full project over from AS3 to
>> haXe.
>>
>> So far it's gone alright, but I'm stuck on a couple things.
>> Perhaps someone could help?
>>
>>
>>
>> 1.) Function type
>>
>>
>> As far as I've seen, there's no way to define a generic
>> function type. Is that true? It seems like there's a way to
>> add a method as a type, but only if you specify the
>> parameters it accepts. If that's true, that really seems
>> like a pain. I have some key instances where it helps to
>> support generic methods. Here's one example:
>>
>>
>> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
>> myParameters);
>>
>>
>>
>> I have a KeyBinding class, which I use to listen to keys
>> and call methods. Would it make sense to strongly type which
>> parameters the target method is expecting? Not at all, I
>> think. How do I get around this?
>>
>>
>>
>>
>> 2.) Rest parameters
>>
>>
>> I also have instances where it's a HUGE help to be able to
>> pass rest parameters. I guess this also isn't possible?
>>
>>
>>
>>
>> 3.) Apply
>>
>>
>> Finally, does something like this work in haXe?
>>
>>
>> myMethod.apply (null, myParameters);
>>
>>
>>
>>
>> Thanks for the help!
>>
>> --haXe - an open source web programming language
>> http://haxe.org
>>
>
>
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
Joshua Granick
Owner / Lead Developer
[ eclecticdesignstudio ]
P: (916) 889-7306

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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

Tony Polinelli
In reply to this post by Lyndon Howie

Rest parameters arnt supported in haxe as they are totally untyped, and can most often be handled in better ways. 

The simplest way is to define a variable which takes an array that holds the vars. The Reflect.callMethod is a good example

in as3 you might have had:  Reflect.callMethod (scope, method, ...args)

In haxe we have:

 Reflect.callMethod (scope, method, args:Array<Dynamic>)

i know that this doesnt seem great at first, but it is only [ ] <- two characters more typing when calling the method.


Reflect.callMethod is the equivalent of apply.

There is also makeVarArgs - but i've never understood it correctly :P


On Mon, Aug 16, 2010 at 5:46 PM, Lyndon Howie <[hidden email]> wrote:
1)
In this case you can do something like:
KeyBinding.addOnPress (Keyboard.LEFT, function () { myFunction(myParameters); });
for similar effect (so your function type would be Void->Void, but you can basically do whatever you want by just wrapping the function and params you want called by a void->void function).
2) Sorry, I don't understand what a rest parameter is...
3) Sorry, also not quite sure what you mean by that either :)

--- On Mon, 16/8/10, Joshua Granick <[hidden email]> wrote:

> From: Joshua Granick <[hidden email]>
> Subject: [haXe] Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?
> To: "The haXe compiler list" <[hidden email]>
> Received: Monday, 16 August, 2010, 5:19 PM
> Hey guys,
>
>
> I'm trying to port my first full project over from AS3 to
> haXe.
>
> So far it's gone alright, but I'm stuck on a couple things.
> Perhaps someone could help?
>
>
>
> 1.) Function type
>
>
> As far as I've seen, there's no way to define a generic
> function type. Is that true? It seems like there's a way to
> add a method as a type, but only if you specify the
> parameters it accepts. If that's true, that really seems
> like a pain. I have some key instances where it helps to
> support generic methods. Here's one example:
>
>
> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
> myParameters);
>
>
>
> I have a KeyBinding class, which I use to listen to keys
> and call methods. Would it make sense to strongly type which
> parameters the target method is expecting? Not at all, I
> think. How do I get around this?
>
>
>
>
> 2.) Rest parameters
>
>
> I also have instances where it's a HUGE help to be able to
> pass rest parameters. I guess this also isn't possible?
>
>
>
>
> 3.) Apply
>
>
> Finally, does something like this work in haXe?
>
>
> myMethod.apply (null, myParameters);
>
>
>
>
> Thanks for the help!
>
> --haXe - an open source web programming language
> http://haxe.org
>




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



--
Tony Polinelli
http://touchmypixel.com

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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

Lyndon Howie
In reply to this post by singmajesty
1) If you need to access instance data, you simply need a reference to the instance. In your example that might be:
var self = this; // get a reference to this object instance
KeyBinding.addOnPress (Keyboard.LEFT, function () {
self.myFunction(self.myParameters); });
Hope that makes sense.


--- On Mon, 16/8/10, Joshua Granick <[hidden email]> wrote:

> From: Joshua Granick <[hidden email]>
> Subject: Re: [haXe] Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?
> To: "The haXe compiler list" <[hidden email]>
> Received: Monday, 16 August, 2010, 5:54 PM
> 1.) I tried to do something like
> that, but it threw an error, saying it couldn't access that
> method in a static function. So far the only way I've been
> able to handle it is to have a class method for every
> possible handler. Sort of a pain, but I guess it works.
> Hopefully there's a more elegant way to handle it?
>
>
> 2.) A "rest" parameter will let you pass an indeterminate
> number of parameters to a method. Here's an example from
> AS3:
>
>
>
> private function myFunction (... parameters:Array) {
>    
>     for each (var parameter:Object in
> parameters) {
>        
>         trace (parameter);
>        
>     }
>    
> }
>
>
> Some of the core methods support "rest" parameters like
> this, like trace(). You can trace ("Hello") or you can trace
> ("Hello", "World", "!");
>
>
> 3.) Apply will let you take an array of parameters, and
> pass them into the function. Here's an example:
>
>
>
> var strings:Array = [ "Hello", "World", "!" ];
>
> trace.apply (null, strings);
>
>
>
> It's useful when used in conjuction with rest parameters.
> You can store an unlimited number of parameters to an array,
> then you can pass those parameters to your target method
>
>
>
>
>
> On Mon, 16 Aug 2010 00:46:35 -0700, Lyndon Howie <[hidden email]>
> wrote:
>
> > 1)
> > In this case you can do something like:
> > KeyBinding.addOnPress (Keyboard.LEFT, function () {
> myFunction(myParameters); });
> > for similar effect (so your function type would be
> Void->Void, but you can basically do whatever you want by
> just wrapping the function and params you want called by a
> void->void function).
> > 2) Sorry, I don't understand what a rest parameter
> is...
> > 3) Sorry, also not quite sure what you mean by that
> either :)
> >
> > --- On Mon, 16/8/10, Joshua Granick <[hidden email]>
> wrote:
> >
> >> From: Joshua Granick <[hidden email]>
> >> Subject: [haXe] Porting from AS3 to haXe: How do I
> replace a Function type, rest parameters, and apply?
> >> To: "The haXe compiler list" <[hidden email]>
> >> Received: Monday, 16 August, 2010, 5:19 PM
> >> Hey guys,
> >>
> >>
> >> I'm trying to port my first full project over from
> AS3 to
> >> haXe.
> >>
> >> So far it's gone alright, but I'm stuck on a
> couple things.
> >> Perhaps someone could help?
> >>
> >>
> >>
> >> 1.) Function type
> >>
> >>
> >> As far as I've seen, there's no way to define a
> generic
> >> function type. Is that true? It seems like there's
> a way to
> >> add a method as a type, but only if you specify
> the
> >> parameters it accepts. If that's true, that really
> seems
> >> like a pain. I have some key instances where it
> helps to
> >> support generic methods. Here's one example:
> >>
> >>
> >> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
> >> myParameters);
> >>
> >>
> >>
> >> I have a KeyBinding class, which I use to listen
> to keys
> >> and call methods. Would it make sense to strongly
> type which
> >> parameters the target method is expecting? Not at
> all, I
> >> think. How do I get around this?
> >>
> >>
> >>
> >>
> >> 2.) Rest parameters
> >>
> >>
> >> I also have instances where it's a HUGE help to be
> able to
> >> pass rest parameters. I guess this also isn't
> possible?
> >>
> >>
> >>
> >>
> >> 3.) Apply
> >>
> >>
> >> Finally, does something like this work in haXe?
> >>
> >>
> >> myMethod.apply (null, myParameters);
> >>
> >>
> >>
> >>
> >> Thanks for the help!
> >>
> >> --haXe - an open source web programming language
> >> http://haxe.org
> >>
> >
> >
> >
> >
> > --
> > haXe - an open source web programming language
> > http://haxe.org
>
>
> --Joshua Granick
> Owner / Lead Developer
> [ eclecticdesignstudio ]
> P: (916) 889-7306
>
> --haXe - an open source web programming language
> http://haxe.org
>




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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

Tony Polinelli
Instead of sending in an anonymous function to the function, send in a method of you class, eg:

public function new()
{
    KeyBinding.addOnPress (Keyboard.LEFT, goLeft);
}

public function goLeft(e) { ... }  - 

it will work as expected (be able to access the whole class)
 

On Mon, Aug 16, 2010 at 6:07 PM, Lyndon Howie <[hidden email]> wrote:
1) If you need to access instance data, you simply need a reference to the instance. In your example that might be:
var self = this; // get a reference to this object instance
KeyBinding.addOnPress (Keyboard.LEFT, function () {
self.myFunction(self.myParameters); });
Hope that makes sense.


--- On Mon, 16/8/10, Joshua Granick <[hidden email]> wrote:

> From: Joshua Granick <[hidden email]>
> Subject: Re: [haXe] Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?
> To: "The haXe compiler list" <[hidden email]>
> Received: Monday, 16 August, 2010, 5:54 PM
> 1.) I tried to do something like
> that, but it threw an error, saying it couldn't access that
> method in a static function. So far the only way I've been
> able to handle it is to have a class method for every
> possible handler. Sort of a pain, but I guess it works.
> Hopefully there's a more elegant way to handle it?
>
>
> 2.) A "rest" parameter will let you pass an indeterminate
> number of parameters to a method. Here's an example from
> AS3:
>
>
>
> private function myFunction (... parameters:Array) {
>    
>     for each (var parameter:Object in
> parameters) {
>        
>         trace (parameter);
>        
>     }
>    
> }
>
>
> Some of the core methods support "rest" parameters like
> this, like trace(). You can trace ("Hello") or you can trace
> ("Hello", "World", "!");
>
>
> 3.) Apply will let you take an array of parameters, and
> pass them into the function. Here's an example:
>
>
>
> var strings:Array = [ "Hello", "World", "!" ];
>
> trace.apply (null, strings);
>
>
>
> It's useful when used in conjuction with rest parameters.
> You can store an unlimited number of parameters to an array,
> then you can pass those parameters to your target method
>
>
>
>
>
> On Mon, 16 Aug 2010 00:46:35 -0700, Lyndon Howie <[hidden email]>
> wrote:
>
> > 1)
> > In this case you can do something like:
> > KeyBinding.addOnPress (Keyboard.LEFT, function () {
> myFunction(myParameters); });
> > for similar effect (so your function type would be
> Void->Void, but you can basically do whatever you want by
> just wrapping the function and params you want called by a
> void->void function).
> > 2) Sorry, I don't understand what a rest parameter
> is...
> > 3) Sorry, also not quite sure what you mean by that
> either :)
> >
> > --- On Mon, 16/8/10, Joshua Granick <[hidden email]>
> wrote:
> >
> >> From: Joshua Granick <[hidden email]>
> >> Subject: [haXe] Porting from AS3 to haXe: How do I
> replace a Function type, rest parameters, and apply?
> >> To: "The haXe compiler list" <[hidden email]>
> >> Received: Monday, 16 August, 2010, 5:19 PM
> >> Hey guys,
> >>
> >>
> >> I'm trying to port my first full project over from
> AS3 to
> >> haXe.
> >>
> >> So far it's gone alright, but I'm stuck on a
> couple things.
> >> Perhaps someone could help?
> >>
> >>
> >>
> >> 1.) Function type
> >>
> >>
> >> As far as I've seen, there's no way to define a
> generic
> >> function type. Is that true? It seems like there's
> a way to
> >> add a method as a type, but only if you specify
> the
> >> parameters it accepts. If that's true, that really
> seems
> >> like a pain. I have some key instances where it
> helps to
> >> support generic methods. Here's one example:
> >>
> >>
> >> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
> >> myParameters);
> >>
> >>
> >>
> >> I have a KeyBinding class, which I use to listen
> to keys
> >> and call methods. Would it make sense to strongly
> type which
> >> parameters the target method is expecting? Not at
> all, I
> >> think. How do I get around this?
> >>
> >>
> >>
> >>
> >> 2.) Rest parameters
> >>
> >>
> >> I also have instances where it's a HUGE help to be
> able to
> >> pass rest parameters. I guess this also isn't
> possible?
> >>
> >>
> >>
> >>
> >> 3.) Apply
> >>
> >>
> >> Finally, does something like this work in haXe?
> >>
> >>
> >> myMethod.apply (null, myParameters);
> >>
> >>
> >>
> >>
> >> Thanks for the help!
> >>
> >> --haXe - an open source web programming language
> >> http://haxe.org
> >>
> >
> >
> >
> >
> > --
> > haXe - an open source web programming language
> > http://haxe.org
>
>
> --Joshua Granick
> Owner / Lead Developer
> [ eclecticdesignstudio ]
> P: (916) 889-7306
>
> --haXe - an open source web programming language
> http://haxe.org
>




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



--
Tony Polinelli
http://touchmypixel.com

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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

jlm@justinfront.net
In reply to this post by Lyndon Howie
1) Carefully read http://haxe.org/ref/type_advanced function section  
it says the same thing in a different way.
2)3) Use Reflection http://haxe.org/doc/flash/as2_compare see function  
section, also see http://markmail.org/message/sferjofbnduj2sdn

On 16 Aug 2010, at 08:46, Lyndon Howie wrote:

> 1)
> In this case you can do something like:
> KeyBinding.addOnPress (Keyboard.LEFT, function ()  
> { myFunction(myParameters); });
> for similar effect (so your function type would be Void->Void, but  
> you can basically do whatever you want by just wrapping the function  
> and params you want called by a void->void function).
> 2) Sorry, I don't understand what a rest parameter is...
> 3) Sorry, also not quite sure what you mean by that either :)
>
> --- On Mon, 16/8/10, Joshua Granick <[hidden email]>  
> wrote:
>
>> From: Joshua Granick <[hidden email]>
>> Subject: [haXe] Porting from AS3 to haXe: How do I replace a  
>> Function type, rest parameters, and apply?
>> To: "The haXe compiler list" <[hidden email]>
>> Received: Monday, 16 August, 2010, 5:19 PM
>> Hey guys,
>>
>>
>> I'm trying to port my first full project over from AS3 to
>> haXe.
>>
>> So far it's gone alright, but I'm stuck on a couple things.
>> Perhaps someone could help?
>>
>>
>>
>> 1.) Function type
>>
>>
>> As far as I've seen, there's no way to define a generic
>> function type. Is that true? It seems like there's a way to
>> add a method as a type, but only if you specify the
>> parameters it accepts. If that's true, that really seems
>> like a pain. I have some key instances where it helps to
>> support generic methods. Here's one example:
>>
>>
>> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
>> myParameters);
>>
>>
>>
>> I have a KeyBinding class, which I use to listen to keys
>> and call methods. Would it make sense to strongly type which
>> parameters the target method is expecting? Not at all, I
>> think. How do I get around this?
>>
>>
>>
>>
>> 2.) Rest parameters
>>
>>
>> I also have instances where it's a HUGE help to be able to
>> pass rest parameters. I guess this also isn't possible?
>>
>>
>>
>>
>> 3.) Apply
>>
>>
>> Finally, does something like this work in haXe?
>>
>>
>> myMethod.apply (null, myParameters);
>>
>>
>>
>>
>> Thanks for the help!
>>
>> --haXe - an open source web programming language
>> http://haxe.org
>>
>
>
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

singmajesty
In reply to this post by Lyndon Howie
Oh, right. Reminds me of working with Actionscript 1.


On Mon, 16 Aug 2010 01:07:24 -0700, Lyndon Howie  
<[hidden email]> wrote:

> 1) If you need to access instance data, you simply need a reference to  
> the instance. In your example that might be:
> var self = this; // get a reference to this object instance
> KeyBinding.addOnPress (Keyboard.LEFT, function () {
> self.myFunction(self.myParameters); });
> Hope that makes sense.
>
>
> --- On Mon, 16/8/10, Joshua Granick <[hidden email]> wrote:
>
>> From: Joshua Granick <[hidden email]>
>> Subject: Re: [haXe] Porting from AS3 to haXe: How do I replace a  
>> Function type, rest parameters, and apply?
>> To: "The haXe compiler list" <[hidden email]>
>> Received: Monday, 16 August, 2010, 5:54 PM
>> 1.) I tried to do something like
>> that, but it threw an error, saying it couldn't access that
>> method in a static function. So far the only way I've been
>> able to handle it is to have a class method for every
>> possible handler. Sort of a pain, but I guess it works.
>> Hopefully there's a more elegant way to handle it?
>>
>>
>> 2.) A "rest" parameter will let you pass an indeterminate
>> number of parameters to a method. Here's an example from
>> AS3:
>>
>>
>>
>> private function myFunction (... parameters:Array) {
>>       for each (var parameter:Object in
>> parameters) {
>>               trace (parameter);
>>           }
>>   }
>>
>>
>> Some of the core methods support "rest" parameters like
>> this, like trace(). You can trace ("Hello") or you can trace
>> ("Hello", "World", "!");
>>
>>
>> 3.) Apply will let you take an array of parameters, and
>> pass them into the function. Here's an example:
>>
>>
>>
>> var strings:Array = [ "Hello", "World", "!" ];
>>
>> trace.apply (null, strings);
>>
>>
>>
>> It's useful when used in conjuction with rest parameters.
>> You can store an unlimited number of parameters to an array,
>> then you can pass those parameters to your target method
>>
>>
>>
>>
>>
>> On Mon, 16 Aug 2010 00:46:35 -0700, Lyndon Howie  
>> <[hidden email]>
>> wrote:
>>
>> > 1)
>> > In this case you can do something like:
>> > KeyBinding.addOnPress (Keyboard.LEFT, function () {
>> myFunction(myParameters); });
>> > for similar effect (so your function type would be
>> Void->Void, but you can basically do whatever you want by
>> just wrapping the function and params you want called by a
>> void->void function).
>> > 2) Sorry, I don't understand what a rest parameter
>> is...
>> > 3) Sorry, also not quite sure what you mean by that
>> either :)
>> >
>> > --- On Mon, 16/8/10, Joshua Granick <[hidden email]>
>> wrote:
>> >
>> >> From: Joshua Granick <[hidden email]>
>> >> Subject: [haXe] Porting from AS3 to haXe: How do I
>> replace a Function type, rest parameters, and apply?
>> >> To: "The haXe compiler list" <[hidden email]>
>> >> Received: Monday, 16 August, 2010, 5:19 PM
>> >> Hey guys,
>> >>
>> >>
>> >> I'm trying to port my first full project over from
>> AS3 to
>> >> haXe.
>> >>
>> >> So far it's gone alright, but I'm stuck on a
>> couple things.
>> >> Perhaps someone could help?
>> >>
>> >>
>> >>
>> >> 1.) Function type
>> >>
>> >>
>> >> As far as I've seen, there's no way to define a
>> generic
>> >> function type. Is that true? It seems like there's
>> a way to
>> >> add a method as a type, but only if you specify
>> the
>> >> parameters it accepts. If that's true, that really
>> seems
>> >> like a pain. I have some key instances where it
>> helps to
>> >> support generic methods. Here's one example:
>> >>
>> >>
>> >> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
>> >> myParameters);
>> >>
>> >>
>> >>
>> >> I have a KeyBinding class, which I use to listen
>> to keys
>> >> and call methods. Would it make sense to strongly
>> type which
>> >> parameters the target method is expecting? Not at
>> all, I
>> >> think. How do I get around this?
>> >>
>> >>
>> >>
>> >>
>> >> 2.) Rest parameters
>> >>
>> >>
>> >> I also have instances where it's a HUGE help to be
>> able to
>> >> pass rest parameters. I guess this also isn't
>> possible?
>> >>
>> >>
>> >>
>> >>
>> >> 3.) Apply
>> >>
>> >>
>> >> Finally, does something like this work in haXe?
>> >>
>> >>
>> >> myMethod.apply (null, myParameters);
>> >>
>> >>
>> >>
>> >>
>> >> Thanks for the help!
>> >>
>> >> --haXe - an open source web programming language
>> >> http://haxe.org
>> >>
>> >
>> >
>> >
>> >
>> > --
>> > haXe - an open source web programming language
>> > http://haxe.org
>>
>>
>> --Joshua Granick
>> Owner / Lead Developer
>> [ eclecticdesignstudio ]
>> P: (916) 889-7306
>>
>> --haXe - an open source web programming language
>> http://haxe.org
>>
>
>
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
Joshua Granick
Owner / Lead Developer
[ eclecticdesignstudio ]
P: (916) 889-7306

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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

jlm@justinfront.net

KeyBinding.addOnPress( Keyboard.LEFT, callback( f( here )
{ here.myParameter }, this ) };

haXe reduces the need for Keyboard.LEFT static strings as you can use  
Enums but not so easy when relying on what flash gives you not sure if  
converting events into HSL would remove these Strings and replace them  
with enums.


On 16 Aug 2010, at 09:15, Joshua Granick wrote:

> Oh, right. Reminds me of working with Actionscript 1.
>
>
> On Mon, 16 Aug 2010 01:07:24 -0700, Lyndon Howie <[hidden email]
> > wrote:
>
>> 1) If you need to access instance data, you simply need a reference  
>> to the instance. In your example that might be:
>> var self = this; // get a reference to this object instance
>> KeyBinding.addOnPress (Keyboard.LEFT, function () {
>> self.myFunction(self.myParameters); });
>> Hope that makes sense.
>>
>>
>> --- On Mon, 16/8/10, Joshua Granick <[hidden email]>  
>> wrote:
>>
>>> From: Joshua Granick <[hidden email]>
>>> Subject: Re: [haXe] Porting from AS3 to haXe: How do I replace a  
>>> Function type, rest parameters, and apply?
>>> To: "The haXe compiler list" <[hidden email]>
>>> Received: Monday, 16 August, 2010, 5:54 PM
>>> 1.) I tried to do something like
>>> that, but it threw an error, saying it couldn't access that
>>> method in a static function. So far the only way I've been
>>> able to handle it is to have a class method for every
>>> possible handler. Sort of a pain, but I guess it works.
>>> Hopefully there's a more elegant way to handle it?
>>>
>>>
>>> 2.) A "rest" parameter will let you pass an indeterminate
>>> number of parameters to a method. Here's an example from
>>> AS3:
>>>
>>>
>>>
>>> private function myFunction (... parameters:Array) {
>>>      for each (var parameter:Object in
>>> parameters) {
>>>              trace (parameter);
>>>          }
>>>  }
>>>
>>>
>>> Some of the core methods support "rest" parameters like
>>> this, like trace(). You can trace ("Hello") or you can trace
>>> ("Hello", "World", "!");
>>>
>>>
>>> 3.) Apply will let you take an array of parameters, and
>>> pass them into the function. Here's an example:
>>>
>>>
>>>
>>> var strings:Array = [ "Hello", "World", "!" ];
>>>
>>> trace.apply (null, strings);
>>>
>>>
>>>
>>> It's useful when used in conjuction with rest parameters.
>>> You can store an unlimited number of parameters to an array,
>>> then you can pass those parameters to your target method
>>>
>>>
>>>
>>>
>>>
>>> On Mon, 16 Aug 2010 00:46:35 -0700, Lyndon Howie <[hidden email]
>>> >
>>> wrote:
>>>
>>> > 1)
>>> > In this case you can do something like:
>>> > KeyBinding.addOnPress (Keyboard.LEFT, function () {
>>> myFunction(myParameters); });
>>> > for similar effect (so your function type would be
>>> Void->Void, but you can basically do whatever you want by
>>> just wrapping the function and params you want called by a
>>> void->void function).
>>> > 2) Sorry, I don't understand what a rest parameter
>>> is...
>>> > 3) Sorry, also not quite sure what you mean by that
>>> either :)
>>> >
>>> > --- On Mon, 16/8/10, Joshua Granick <[hidden email]>
>>> wrote:
>>> >
>>> >> From: Joshua Granick <[hidden email]>
>>> >> Subject: [haXe] Porting from AS3 to haXe: How do I
>>> replace a Function type, rest parameters, and apply?
>>> >> To: "The haXe compiler list" <[hidden email]>
>>> >> Received: Monday, 16 August, 2010, 5:19 PM
>>> >> Hey guys,
>>> >>
>>> >>
>>> >> I'm trying to port my first full project over from
>>> AS3 to
>>> >> haXe.
>>> >>
>>> >> So far it's gone alright, but I'm stuck on a
>>> couple things.
>>> >> Perhaps someone could help?
>>> >>
>>> >>
>>> >>
>>> >> 1.) Function type
>>> >>
>>> >>
>>> >> As far as I've seen, there's no way to define a
>>> generic
>>> >> function type. Is that true? It seems like there's
>>> a way to
>>> >> add a method as a type, but only if you specify
>>> the
>>> >> parameters it accepts. If that's true, that really
>>> seems
>>> >> like a pain. I have some key instances where it
>>> helps to
>>> >> support generic methods. Here's one example:
>>> >>
>>> >>
>>> >> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
>>> >> myParameters);
>>> >>
>>> >>
>>> >>
>>> >> I have a KeyBinding class, which I use to listen
>>> to keys
>>> >> and call methods. Would it make sense to strongly
>>> type which
>>> >> parameters the target method is expecting? Not at
>>> all, I
>>> >> think. How do I get around this?
>>> >>
>>> >>
>>> >>
>>> >>
>>> >> 2.) Rest parameters
>>> >>
>>> >>
>>> >> I also have instances where it's a HUGE help to be
>>> able to
>>> >> pass rest parameters. I guess this also isn't
>>> possible?
>>> >>
>>> >>
>>> >>
>>> >>
>>> >> 3.) Apply
>>> >>
>>> >>
>>> >> Finally, does something like this work in haXe?
>>> >>
>>> >>
>>> >> myMethod.apply (null, myParameters);
>>> >>
>>> >>
>>> >>
>>> >>
>>> >> Thanks for the help!
>>> >>
>>> >> --haXe - an open source web programming language
>>> >> http://haxe.org
>>> >>
>>> >
>>> >
>>> >
>>> >
>>> > --
>>> > haXe - an open source web programming language
>>> > http://haxe.org
>>>
>>>
>>> --Joshua Granick
>>> Owner / Lead Developer
>>> [ eclecticdesignstudio ]
>>> P: (916) 889-7306
>>>
>>> --haXe - an open source web programming language
>>> http://haxe.org
>>>
>>
>>
>>
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> Joshua Granick
> Owner / Lead Developer
> [ eclecticdesignstudio ]
> P: (916) 889-7306
>
> --
> haXe - an open source web programming language
> http://haxe.org


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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

singmajesty
Awesome!

callback is precisely what I needed:


KeyBinding.addOnPress (Keyboard.LEFT, callback (toggleKey, Keyboard.LEFT,  
true));



That makes sense. I figured that there had to be some way to handle  
callbacks. It would be a pain to have to hard-code everything to the same  
method signature without something like this in place.

Great! Thanks again




On Mon, 16 Aug 2010 01:24:17 -0700, [hidden email]  
<[hidden email]> wrote:

>
> KeyBinding.addOnPress( Keyboard.LEFT, callback( f( here ){  
> here.myParameter }, this ) };
>
> haXe reduces the need for Keyboard.LEFT static strings as you can use  
> Enums but not so easy when relying on what flash gives you not sure if  
> converting events into HSL would remove these Strings and replace them  
> with enums.
>
>
> On 16 Aug 2010, at 09:15, Joshua Granick wrote:
>
>> Oh, right. Reminds me of working with Actionscript 1.
>>
>>
>> On Mon, 16 Aug 2010 01:07:24 -0700, Lyndon Howie  
>> <[hidden email]> wrote:
>>
>>> 1) If you need to access instance data, you simply need a reference to  
>>> the instance. In your example that might be:
>>> var self = this; // get a reference to this object instance
>>> KeyBinding.addOnPress (Keyboard.LEFT, function () {
>>> self.myFunction(self.myParameters); });
>>> Hope that makes sense.
>>>
>>>
>>> --- On Mon, 16/8/10, Joshua Granick <[hidden email]> wrote:
>>>
>>>> From: Joshua Granick <[hidden email]>
>>>> Subject: Re: [haXe] Porting from AS3 to haXe: How do I replace a  
>>>> Function type, rest parameters, and apply?
>>>> To: "The haXe compiler list" <[hidden email]>
>>>> Received: Monday, 16 August, 2010, 5:54 PM
>>>> 1.) I tried to do something like
>>>> that, but it threw an error, saying it couldn't access that
>>>> method in a static function. So far the only way I've been
>>>> able to handle it is to have a class method for every
>>>> possible handler. Sort of a pain, but I guess it works.
>>>> Hopefully there's a more elegant way to handle it?
>>>>
>>>>
>>>> 2.) A "rest" parameter will let you pass an indeterminate
>>>> number of parameters to a method. Here's an example from
>>>> AS3:
>>>>
>>>>
>>>>
>>>> private function myFunction (... parameters:Array) {
>>>>      for each (var parameter:Object in
>>>> parameters) {
>>>>              trace (parameter);
>>>>          }
>>>>  }
>>>>
>>>>
>>>> Some of the core methods support "rest" parameters like
>>>> this, like trace(). You can trace ("Hello") or you can trace
>>>> ("Hello", "World", "!");
>>>>
>>>>
>>>> 3.) Apply will let you take an array of parameters, and
>>>> pass them into the function. Here's an example:
>>>>
>>>>
>>>>
>>>> var strings:Array = [ "Hello", "World", "!" ];
>>>>
>>>> trace.apply (null, strings);
>>>>
>>>>
>>>>
>>>> It's useful when used in conjuction with rest parameters.
>>>> You can store an unlimited number of parameters to an array,
>>>> then you can pass those parameters to your target method
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Mon, 16 Aug 2010 00:46:35 -0700, Lyndon Howie  
>>>> <[hidden email]>
>>>> wrote:
>>>>
>>>> > 1)
>>>> > In this case you can do something like:
>>>> > KeyBinding.addOnPress (Keyboard.LEFT, function () {
>>>> myFunction(myParameters); });
>>>> > for similar effect (so your function type would be
>>>> Void->Void, but you can basically do whatever you want by
>>>> just wrapping the function and params you want called by a
>>>> void->void function).
>>>> > 2) Sorry, I don't understand what a rest parameter
>>>> is...
>>>> > 3) Sorry, also not quite sure what you mean by that
>>>> either :)
>>>> >
>>>> > --- On Mon, 16/8/10, Joshua Granick <[hidden email]>
>>>> wrote:
>>>> >
>>>> >> From: Joshua Granick <[hidden email]>
>>>> >> Subject: [haXe] Porting from AS3 to haXe: How do I
>>>> replace a Function type, rest parameters, and apply?
>>>> >> To: "The haXe compiler list" <[hidden email]>
>>>> >> Received: Monday, 16 August, 2010, 5:19 PM
>>>> >> Hey guys,
>>>> >>
>>>> >>
>>>> >> I'm trying to port my first full project over from
>>>> AS3 to
>>>> >> haXe.
>>>> >>
>>>> >> So far it's gone alright, but I'm stuck on a
>>>> couple things.
>>>> >> Perhaps someone could help?
>>>> >>
>>>> >>
>>>> >>
>>>> >> 1.) Function type
>>>> >>
>>>> >>
>>>> >> As far as I've seen, there's no way to define a
>>>> generic
>>>> >> function type. Is that true? It seems like there's
>>>> a way to
>>>> >> add a method as a type, but only if you specify
>>>> the
>>>> >> parameters it accepts. If that's true, that really
>>>> seems
>>>> >> like a pain. I have some key instances where it
>>>> helps to
>>>> >> support generic methods. Here's one example:
>>>> >>
>>>> >>
>>>> >> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
>>>> >> myParameters);
>>>> >>
>>>> >>
>>>> >>
>>>> >> I have a KeyBinding class, which I use to listen
>>>> to keys
>>>> >> and call methods. Would it make sense to strongly
>>>> type which
>>>> >> parameters the target method is expecting? Not at
>>>> all, I
>>>> >> think. How do I get around this?
>>>> >>
>>>> >>
>>>> >>
>>>> >>
>>>> >> 2.) Rest parameters
>>>> >>
>>>> >>
>>>> >> I also have instances where it's a HUGE help to be
>>>> able to
>>>> >> pass rest parameters. I guess this also isn't
>>>> possible?
>>>> >>
>>>> >>
>>>> >>
>>>> >>
>>>> >> 3.) Apply
>>>> >>
>>>> >>
>>>> >> Finally, does something like this work in haXe?
>>>> >>
>>>> >>
>>>> >> myMethod.apply (null, myParameters);
>>>> >>
>>>> >>
>>>> >>
>>>> >>
>>>> >> Thanks for the help!
>>>> >>
>>>> >> --haXe - an open source web programming language
>>>> >> http://haxe.org
>>>> >>
>>>> >
>>>> >
>>>> >
>>>> >
>>>> > --
>>>> > haXe - an open source web programming language
>>>> > http://haxe.org
>>>>
>>>>
>>>> --Joshua Granick
>>>> Owner / Lead Developer
>>>> [ eclecticdesignstudio ]
>>>> P: (916) 889-7306
>>>>
>>>> --haXe - an open source web programming language
>>>> http://haxe.org
>>>>
>>>
>>>
>>>
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>> --Joshua Granick
>> Owner / Lead Developer
>> [ eclecticdesignstudio ]
>> P: (916) 889-7306
>>
>> --haXe - an open source web programming language
>> http://haxe.org
>
>


--
Joshua Granick
Owner / Lead Developer
[ eclecticdesignstudio ]
P: (916) 889-7306

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

Re: Porting from AS3 to haXe: How do I replace a Function type, rest parameters, and apply?

jlm@justinfront.net
and in as3 I would use wildwinters.Callback.create, some regard them  
as hacky but I find it saves creating a class, and too much  
abstraction in Gui's quickly become unusable.

On 16 Aug 2010, at 10:48, Joshua Granick wrote:

> Awesome!
>
> callback is precisely what I needed:
>
>
> KeyBinding.addOnPress (Keyboard.LEFT, callback (toggleKey,  
> Keyboard.LEFT, true));
>
>
>
> That makes sense. I figured that there had to be some way to handle  
> callbacks. It would be a pain to have to hard-code everything to the  
> same method signature without something like this in place.
>
> Great! Thanks again
>
>
>
>
> On Mon, 16 Aug 2010 01:24:17 -0700, [hidden email] <[hidden email]
> > wrote:
>
>>
>> KeyBinding.addOnPress( Keyboard.LEFT, callback( f( here )
>> { here.myParameter }, this ) };
>>
>> haXe reduces the need for Keyboard.LEFT static strings as you can  
>> use Enums but not so easy when relying on what flash gives you not  
>> sure if converting events into HSL would remove these Strings and  
>> replace them with enums.
>>
>>
>> On 16 Aug 2010, at 09:15, Joshua Granick wrote:
>>
>>> Oh, right. Reminds me of working with Actionscript 1.
>>>
>>>
>>> On Mon, 16 Aug 2010 01:07:24 -0700, Lyndon Howie <[hidden email]
>>> > wrote:
>>>
>>>> 1) If you need to access instance data, you simply need a  
>>>> reference to the instance. In your example that might be:
>>>> var self = this; // get a reference to this object instance
>>>> KeyBinding.addOnPress (Keyboard.LEFT, function () {
>>>> self.myFunction(self.myParameters); });
>>>> Hope that makes sense.
>>>>
>>>>
>>>> --- On Mon, 16/8/10, Joshua Granick <[hidden email]>  
>>>> wrote:
>>>>
>>>>> From: Joshua Granick <[hidden email]>
>>>>> Subject: Re: [haXe] Porting from AS3 to haXe: How do I replace a  
>>>>> Function type, rest parameters, and apply?
>>>>> To: "The haXe compiler list" <[hidden email]>
>>>>> Received: Monday, 16 August, 2010, 5:54 PM
>>>>> 1.) I tried to do something like
>>>>> that, but it threw an error, saying it couldn't access that
>>>>> method in a static function. So far the only way I've been
>>>>> able to handle it is to have a class method for every
>>>>> possible handler. Sort of a pain, but I guess it works.
>>>>> Hopefully there's a more elegant way to handle it?
>>>>>
>>>>>
>>>>> 2.) A "rest" parameter will let you pass an indeterminate
>>>>> number of parameters to a method. Here's an example from
>>>>> AS3:
>>>>>
>>>>>
>>>>>
>>>>> private function myFunction (... parameters:Array) {
>>>>>     for each (var parameter:Object in
>>>>> parameters) {
>>>>>             trace (parameter);
>>>>>         }
>>>>> }
>>>>>
>>>>>
>>>>> Some of the core methods support "rest" parameters like
>>>>> this, like trace(). You can trace ("Hello") or you can trace
>>>>> ("Hello", "World", "!");
>>>>>
>>>>>
>>>>> 3.) Apply will let you take an array of parameters, and
>>>>> pass them into the function. Here's an example:
>>>>>
>>>>>
>>>>>
>>>>> var strings:Array = [ "Hello", "World", "!" ];
>>>>>
>>>>> trace.apply (null, strings);
>>>>>
>>>>>
>>>>>
>>>>> It's useful when used in conjuction with rest parameters.
>>>>> You can store an unlimited number of parameters to an array,
>>>>> then you can pass those parameters to your target method
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Mon, 16 Aug 2010 00:46:35 -0700, Lyndon Howie <[hidden email]
>>>>> >
>>>>> wrote:
>>>>>
>>>>> > 1)
>>>>> > In this case you can do something like:
>>>>> > KeyBinding.addOnPress (Keyboard.LEFT, function () {
>>>>> myFunction(myParameters); });
>>>>> > for similar effect (so your function type would be
>>>>> Void->Void, but you can basically do whatever you want by
>>>>> just wrapping the function and params you want called by a
>>>>> void->void function).
>>>>> > 2) Sorry, I don't understand what a rest parameter
>>>>> is...
>>>>> > 3) Sorry, also not quite sure what you mean by that
>>>>> either :)
>>>>> >
>>>>> > --- On Mon, 16/8/10, Joshua Granick <[hidden email]>
>>>>> wrote:
>>>>> >
>>>>> >> From: Joshua Granick <[hidden email]>
>>>>> >> Subject: [haXe] Porting from AS3 to haXe: How do I
>>>>> replace a Function type, rest parameters, and apply?
>>>>> >> To: "The haXe compiler list" <[hidden email]>
>>>>> >> Received: Monday, 16 August, 2010, 5:19 PM
>>>>> >> Hey guys,
>>>>> >>
>>>>> >>
>>>>> >> I'm trying to port my first full project over from
>>>>> AS3 to
>>>>> >> haXe.
>>>>> >>
>>>>> >> So far it's gone alright, but I'm stuck on a
>>>>> couple things.
>>>>> >> Perhaps someone could help?
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >> 1.) Function type
>>>>> >>
>>>>> >>
>>>>> >> As far as I've seen, there's no way to define a
>>>>> generic
>>>>> >> function type. Is that true? It seems like there's
>>>>> a way to
>>>>> >> add a method as a type, but only if you specify
>>>>> the
>>>>> >> parameters it accepts. If that's true, that really
>>>>> seems
>>>>> >> like a pain. I have some key instances where it
>>>>> helps to
>>>>> >> support generic methods. Here's one example:
>>>>> >>
>>>>> >>
>>>>> >> KeyBinding.addOnPress (Keyboard.LEFT, myFunction,
>>>>> >> myParameters);
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >> I have a KeyBinding class, which I use to listen
>>>>> to keys
>>>>> >> and call methods. Would it make sense to strongly
>>>>> type which
>>>>> >> parameters the target method is expecting? Not at
>>>>> all, I
>>>>> >> think. How do I get around this?
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >> 2.) Rest parameters
>>>>> >>
>>>>> >>
>>>>> >> I also have instances where it's a HUGE help to be
>>>>> able to
>>>>> >> pass rest parameters. I guess this also isn't
>>>>> possible?
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >> 3.) Apply
>>>>> >>
>>>>> >>
>>>>> >> Finally, does something like this work in haXe?
>>>>> >>
>>>>> >>
>>>>> >> myMethod.apply (null, myParameters);
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >> Thanks for the help!
>>>>> >>
>>>>> >> --haXe - an open source web programming language
>>>>> >> http://haxe.org
>>>>> >>
>>>>> >
>>>>> >
>>>>> >
>>>>> >
>>>>> > --
>>>>> > haXe - an open source web programming language
>>>>> > http://haxe.org
>>>>>
>>>>>
>>>>> --Joshua Granick
>>>>> Owner / Lead Developer
>>>>> [ eclecticdesignstudio ]
>>>>> P: (916) 889-7306
>>>>>
>>>>> --haXe - an open source web programming language
>>>>> http://haxe.org
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> haXe - an open source web programming language
>>>> http://haxe.org
>>>
>>>
>>> --Joshua Granick
>>> Owner / Lead Developer
>>> [ eclecticdesignstudio ]
>>> P: (916) 889-7306
>>>
>>> --haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>
>
> --
> Joshua Granick
> Owner / Lead Developer
> [ eclecticdesignstudio ]
> P: (916) 889-7306
>
> --
> haXe - an open source web programming language
> http://haxe.org


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

Re: indeterminate amount of parameters

Nathan
In reply to this post by Ron Wheeler
nah it's for extern interfaces sadly, can't modify them.

cheers,

nathan

Ron Wheeler wrote:

>  Can you not just pass an array of strings as a single argument?
>
> Ron
>
> On 15/08/2010 8:40 PM, Justin Donaldson wrote:
>> yes, I have this problem too occasionally.  Typically haxe externs
>> will include explicit argument definitions, but only for the first few
>> optional arguments (usually up to 6).  The only other way (afaik) is
>> to type the extern function as Dynamic.
>>
>> -Justin
>>
>>
>> On Sun, Aug 15, 2010 at 5:06 PM, Nathan <[hidden email]
>> <mailto:[hidden email]>> wrote:
>>
>>     Nathan wrote:
>>
>>         Hi All,
>>
>>         Wondering if there's a way to have an indeterminate amount of
>>         parameters on a function/method in haXe. Essentially I want to
>>         have 1...* typed params on a function
>>
>>         Class Foo {
>>          public function write( text:String... ) : Void {}
>>
>>         Where you could pass in any number of String's the method and
>>         it would be happy (but would fail with a diff type)
>>
>>          var o:Foo = new Foo();
>>          o.write( "hello", "there", "haxe" );
>>
>>         Possible?
>>
>>
>>     I've just been pointed to makeVarArgs in the reflection api (by
>>     jan_flanders in irc#haxe), which is great - the caveat is that I
>>     should have mentioned this is for an extern class/interface that
>>     I'm not implementing directly. Thus afaict, the reflection
>>     makeVarArgs method won't work and I'm needing some syntax-sugar.
>>
>>     Cheers,
>>
>>
>>     Nathan
>>
>>
>>
>>     --     haXe - an open source web programming language
>>     http://haxe.org
>>
>>
>>
>>
>> --
>> blog: http://www.scwn.net
>> aim: iujjd
>> twitter: jjdonald
>>
>
>


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

Re: indeterminate amount of parameters

Heinz Hölzer-2
One possibility is to add as many optional params as you need:

extern class Foo {

     // function yourFunc (s:String, n:Int, ...args<String>);

     public function yourFunc (s:String, n:Int, ?arg1:String,
?arg2:String, ?arg3:String, ?arg4:String, .....);

     // or for dynamic args
     public function yourFunc (s:String, n:Int, ?arg1:Dynamic,
?arg2:Dynamic, ?arg3:Dynamic, ?arg4:Dynamic, .....);

}

Am 16.08.2010 14:31, schrieb Nathan:

> nah it's for extern interfaces sadly, can't modify them.
>
> cheers,
>
> nathan
>
> Ron Wheeler wrote:
>>  Can you not just pass an array of strings as a single argument?
>>
>> Ron
>>
>> On 15/08/2010 8:40 PM, Justin Donaldson wrote:
>>> yes, I have this problem too occasionally.  Typically haxe externs
>>> will include explicit argument definitions, but only for the first
>>> few optional arguments (usually up to 6).  The only other way
>>> (afaik) is to type the extern function as Dynamic.
>>>
>>> -Justin
>>>
>>>
>>> On Sun, Aug 15, 2010 at 5:06 PM, Nathan <[hidden email]
>>> <mailto:[hidden email]>> wrote:
>>>
>>>     Nathan wrote:
>>>
>>>         Hi All,
>>>
>>>         Wondering if there's a way to have an indeterminate amount of
>>>         parameters on a function/method in haXe. Essentially I want to
>>>         have 1...* typed params on a function
>>>
>>>         Class Foo {
>>>          public function write( text:String... ) : Void {}
>>>
>>>         Where you could pass in any number of String's the method and
>>>         it would be happy (but would fail with a diff type)
>>>
>>>          var o:Foo = new Foo();
>>>          o.write( "hello", "there", "haxe" );
>>>
>>>         Possible?
>>>
>>>
>>>     I've just been pointed to makeVarArgs in the reflection api (by
>>>     jan_flanders in irc#haxe), which is great - the caveat is that I
>>>     should have mentioned this is for an extern class/interface that
>>>     I'm not implementing directly. Thus afaict, the reflection
>>>     makeVarArgs method won't work and I'm needing some syntax-sugar.
>>>
>>>     Cheers,
>>>
>>>
>>>     Nathan
>>>
>>>
>>>
>>>     --     haXe - an open source web programming language
>>>     http://haxe.org
>>>
>>>
>>>
>>>
>>> --
>>> blog: http://www.scwn.net
>>> aim: iujjd
>>> twitter: jjdonald
>>>
>>
>>
>
>


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

Re: indeterminate amount of parameters

Cauê W.
(though this will only work in flash9).

2010/8/16 Heinz Hölzer <[hidden email]>
One possibility is to add as many optional params as you need:

extern class Foo {

   // function yourFunc (s:String, n:Int, ...args<String>);

   public function yourFunc (s:String, n:Int, ?arg1:String, ?arg2:String, ?arg3:String, ?arg4:String, .....);

   // or for dynamic args
   public function yourFunc (s:String, n:Int, ?arg1:Dynamic, ?arg2:Dynamic, ?arg3:Dynamic, ?arg4:Dynamic, .....);

}

Am 16.08.2010 14:31, schrieb Nathan:

nah it's for extern interfaces sadly, can't modify them.

cheers,

nathan

Ron Wheeler wrote:
 Can you not just pass an array of strings as a single argument?

Ron

On 15/08/2010 8:40 PM, Justin Donaldson wrote:
yes, I have this problem too occasionally.  Typically haxe externs will include explicit argument definitions, but only for the first few optional arguments (usually up to 6).  The only other way (afaik) is to type the extern function as Dynamic.

-Justin


On Sun, Aug 15, 2010 at 5:06 PM, Nathan <[hidden email] <mailto:[hidden email]>> wrote:

   Nathan wrote:

       Hi All,

       Wondering if there's a way to have an indeterminate amount of
       parameters on a function/method in haXe. Essentially I want to
       have 1...* typed params on a function

       Class Foo {
        public function write( text:String... ) : Void {}

       Where you could pass in any number of String's the method and
       it would be happy (but would fail with a diff type)

        var o:Foo = new Foo();
        o.write( "hello", "there", "haxe" );

       Possible?


   I've just been pointed to makeVarArgs in the reflection api (by
   jan_flanders in irc#haxe), which is great - the caveat is that I
   should have mentioned this is for an extern class/interface that
   I'm not implementing directly. Thus afaict, the reflection
   makeVarArgs method won't work and I'm needing some syntax-sugar.

   Cheers,


   Nathan



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




--
blog: http://www.scwn.net
aim: iujjd
twitter: jjdonald







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


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