Function type?

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

Function type?

Juan Delgado
Hi there,

Porting some code from AS3, I want to be able to pass any function as
a parameter. In AS3 looks like this:

function wadus(myFunc : Function) : Void;

Can't find a proper way to do this in haXe. Doing something like:

function wadus(myFunc : Void -> Void) : Void;

...limits the signature of the passed function. For a moment I thought
that Type.TFunction would do the trick, but it doesn't.

I'm going Dynamic at the moment, but sure there should be a better way
of doing this?

Thanks,

Juan

--
Juan Delgado - Zárate
http://zarate.tv
http://blog.zarate.tv

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

Re: [haXe] Function type?

Gustavs
Nah, the function types are for when you know for real what the type is. Otherwise 'Dynamic' is fine. See the sig of 'Reflect.callMethod'.
If it makes you feel better, you could define a typedef 'Function' for indicating when an object should be a function.

2010/1/26 Juan Delgado <[hidden email]>
Hi there,

Porting some code from AS3, I want to be able to pass any function as
a parameter. In AS3 looks like this:

function wadus(myFunc : Function) : Void;

Can't find a proper way to do this in haXe. Doing something like:

function wadus(myFunc : Void -> Void) : Void;

...limits the signature of the passed function. For a moment I thought
that Type.TFunction would do the trick, but it doesn't.

I'm going Dynamic at the moment, but sure there should be a better way
of doing this?

Thanks,

Juan

--
Juan Delgado - Zárate
http://zarate.tv
http://blog.zarate.tv

--
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: [haXe] Function type?

Benjamin Dasnois
In reply to this post by Juan Delgado
Hi,


Dynamic->Dynamic will do it. Keep in mind that "Dynamic" also means "any".

I wrote an article several weeks ago on function types :
http://pignoufou.tumblr.com/post/216312249/typing-functions-in-haxe

Regards,

On Tue, Jan 26, 2010 at 3:55 PM, Juan Delgado <[hidden email]> wrote:

> Hi there,
>
> Porting some code from AS3, I want to be able to pass any function as
> a parameter. In AS3 looks like this:
>
> function wadus(myFunc : Function) : Void;
>
> Can't find a proper way to do this in haXe. Doing something like:
>
> function wadus(myFunc : Void -> Void) : Void;
>
> ...limits the signature of the passed function. For a moment I thought
> that Type.TFunction would do the trick, but it doesn't.
>
> I'm going Dynamic at the moment, but sure there should be a better way
> of doing this?
>
> Thanks,
>
> Juan
>
> --
> Juan Delgado - Zárate
> http://zarate.tv
> http://blog.zarate.tv
>
> --
> haXe - an open source web programming language
> http://haxe.org
>



--
DASNOIS Benjamin
http://www.benjamindasnois.com

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

Re: [haXe] Function type?

Justin Donaldson
That'll work for one argument functions, but you'll still have to use just Dynamic for anything with more arguments.

Another good thing to do is check the run-time type of the Dynamic object, and make sure it's a "TFunction".

It'd be nice if there were a syntax to explicitly specify "any" function (rather than just a Dynamic object).... something like  "_->Dynamic". 

-Justin



On Tue, Jan 26, 2010 at 10:05 AM, Benjamin Dasnois <[hidden email]> wrote:
Hi,


Dynamic->Dynamic will do it. Keep in mind that "Dynamic" also means "any".

I wrote an article several weeks ago on function types :
http://pignoufou.tumblr.com/post/216312249/typing-functions-in-haxe

Regards,

On Tue, Jan 26, 2010 at 3:55 PM, Juan Delgado <[hidden email]> wrote:
> Hi there,
>
> Porting some code from AS3, I want to be able to pass any function as
> a parameter. In AS3 looks like this:
>
> function wadus(myFunc : Function) : Void;
>
> Can't find a proper way to do this in haXe. Doing something like:
>
> function wadus(myFunc : Void -> Void) : Void;
>
> ...limits the signature of the passed function. For a moment I thought
> that Type.TFunction would do the trick, but it doesn't.
>
> I'm going Dynamic at the moment, but sure there should be a better way
> of doing this?
>
> Thanks,
>
> Juan
>
> --
> Juan Delgado - Zárate
> http://zarate.tv
> http://blog.zarate.tv
>
> --
> haXe - an open source web programming language
> http://haxe.org
>



--
DASNOIS Benjamin
http://www.benjamindasnois.com

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



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
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: [haXe] Function type?

Cauê W.
In reply to this post by Benjamin Dasnois
The only downside of this approach is that the compiler doesn't identify Void as Dynamic:

var teste:Dynamic->Dynamic = function () {
            trace("func");
        }

will throw Void -> Void should be Dynamic -> Dynamic

But I have the feeling that defining only as Dynamic will have a performance decrease in flash, since in flash there is a Function type , which should speed things up vs dynamic. So if you're making something performance-critical, you're better off with defining Dynamic->Dynamic, and then casting when it fails.

2010/1/26 Benjamin Dasnois <[hidden email]>
Hi,


Dynamic->Dynamic will do it. Keep in mind that "Dynamic" also means "any".

I wrote an article several weeks ago on function types :
http://pignoufou.tumblr.com/post/216312249/typing-functions-in-haxe

Regards,

On Tue, Jan 26, 2010 at 3:55 PM, Juan Delgado <[hidden email]> wrote:
> Hi there,
>
> Porting some code from AS3, I want to be able to pass any function as
> a parameter. In AS3 looks like this:
>
> function wadus(myFunc : Function) : Void;
>
> Can't find a proper way to do this in haXe. Doing something like:
>
> function wadus(myFunc : Void -> Void) : Void;
>
> ...limits the signature of the passed function. For a moment I thought
> that Type.TFunction would do the trick, but it doesn't.
>
> I'm going Dynamic at the moment, but sure there should be a better way
> of doing this?
>
> Thanks,
>
> Juan
>
> --
> Juan Delgado - Zárate
> http://zarate.tv
> http://blog.zarate.tv
>
> --
> haXe - an open source web programming language
> http://haxe.org
>



--
DASNOIS Benjamin
http://www.benjamindasnois.com

--
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: [haXe] Function type?

Nicolas Cannasse
In reply to this post by Juan Delgado
Juan Delgado a écrit :

> Hi there,
>
> Porting some code from AS3, I want to be able to pass any function as
> a parameter. In AS3 looks like this:
>
> function wadus(myFunc : Function) : Void;
>
> Can't find a proper way to do this in haXe. Doing something like:
>
> function wadus(myFunc : Void -> Void) : Void;
>
> ...limits the signature of the passed function. For a moment I thought
> that Type.TFunction would do the trick, but it doesn't.
>
> I'm going Dynamic at the moment, but sure there should be a better way
> of doing this?

A function which you don't know parameters is not very useful to
consider as a type.

Nicolas

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

Re: [haXe] Function type?

Justin Donaldson
Hi Nicolas,

This comes up in the reflect API though, doesn't it?  There's quite a few methods there where a function is expected, but you can pass any object and the compiler won't complain.

static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic

granted, the function would fail on run-time.  However, couldn't the compiler catch these types of errors right away?

There are a few times when I am writing functions that modify other functions, but preserve the original function's argument arity and return values.  For instance, the Glue class:
http://code.google.com/p/haxle/source/browse/trunk/src/org/sugar/sigslot/Glue.hx

To preserve type safety in this case, it's necessary to specify a different function for each arity (I usually stop at 6).  However, internally each function is identical.
If there was a way to represent a function as:  #A->B then a lot of the redundancy could be removed.  In this case, #A here parametrizes the list of argument types, and B indicates just the return type,

There are two opportunities here, the first is to introduce some way of typing a function to simply differentiate it from an object at compile time.  The second would be to make it possible to indicate the type of the list of arguments... so that you could use them elsewhere.  For instance, the glue method could end up looking like:

public static function glue( func1:#A -> B, func2: #A -> C ) : #A -> B {}

You could then use it on any function, with any arity.

This is not a big problem for me, but worth considering.

Best,
Justin


On Tue, Jan 26, 2010 at 10:55 AM, Nicolas Cannasse <[hidden email]> wrote:
Juan Delgado a écrit :

Hi there,

Porting some code from AS3, I want to be able to pass any function as
a parameter. In AS3 looks like this:

function wadus(myFunc : Function) : Void;

Can't find a proper way to do this in haXe. Doing something like:

function wadus(myFunc : Void -> Void) : Void;

...limits the signature of the passed function. For a moment I thought
that Type.TFunction would do the trick, but it doesn't.

I'm going Dynamic at the moment, but sure there should be a better way
of doing this?

A function which you don't know parameters is not very useful to consider as a type.

Nicolas


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



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
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: [haXe] Function type?

Heinz Hölzer-2

Expecting a specific return value is very useful for Binding in general

f.e. bind a method to a setter method
function bind (getterMethod: # -> T, getterArgs:Array<Dynamic>, setterMethod: T -> Void)


Am 26.01.2010 17:54, schrieb Justin Donaldson:
Hi Nicolas,

This comes up in the reflect API though, doesn't it?  There's quite a few methods there where a function is expected, but you can pass any object and the compiler won't complain.

static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic

granted, the function would fail on run-time.  However, couldn't the compiler catch these types of errors right away?

There are a few times when I am writing functions that modify other functions, but preserve the original function's argument arity and return values.  For instance, the Glue class:
http://code.google.com/p/haxle/source/browse/trunk/src/org/sugar/sigslot/Glue.hx

To preserve type safety in this case, it's necessary to specify a different function for each arity (I usually stop at 6).  However, internally each function is identical.
If there was a way to represent a function as:  #A->B then a lot of the redundancy could be removed.  In this case, #A here parametrizes the list of argument types, and B indicates just the return type,

There are two opportunities here, the first is to introduce some way of typing a function to simply differentiate it from an object at compile time.  The second would be to make it possible to indicate the type of the list of arguments... so that you could use them elsewhere.  For instance, the glue method could end up looking like:

public static function glue( func1:#A -> B, func2: #A -> C ) : #A -> B {}

You could then use it on any function, with any arity.

This is not a big problem for me, but worth considering.

Best,
Justin


On Tue, Jan 26, 2010 at 10:55 AM, Nicolas Cannasse <[hidden email]> wrote:
Juan Delgado a écrit :

Hi there,

Porting some code from AS3, I want to be able to pass any function as
a parameter. In AS3 looks like this:

function wadus(myFunc : Function) : Void;

Can't find a proper way to do this in haXe. Doing something like:

function wadus(myFunc : Void -> Void) : Void;

...limits the signature of the passed function. For a moment I thought
that Type.TFunction would do the trick, but it doesn't.

I'm going Dynamic at the moment, but sure there should be a better way
of doing this?

A function which you don't know parameters is not very useful to consider as a type.

Nicolas


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



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
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: [haXe] Function type?

Cauê W.
In reply to this post by Justin Donaldson
Maybe this could also be extended to enums? Wouldn't it be good to have type-safety for them? I know there is the Enum<T> type, but it only defines the "Type" Enum, not the enum instances. Enum instances still need to be Dynamically typed (So you have to check on runtime if an enum instance is of the desired type, if you're working with enums in a more general way).

2010/1/26 Justin Donaldson <[hidden email]>
Hi Nicolas,

This comes up in the reflect API though, doesn't it?  There's quite a few methods there where a function is expected, but you can pass any object and the compiler won't complain.

static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic

granted, the function would fail on run-time.  However, couldn't the compiler catch these types of errors right away?

There are a few times when I am writing functions that modify other functions, but preserve the original function's argument arity and return values.  For instance, the Glue class:
http://code.google.com/p/haxle/source/browse/trunk/src/org/sugar/sigslot/Glue.hx

To preserve type safety in this case, it's necessary to specify a different function for each arity (I usually stop at 6).  However, internally each function is identical.
If there was a way to represent a function as:  #A->B then a lot of the redundancy could be removed.  In this case, #A here parametrizes the list of argument types, and B indicates just the return type,

There are two opportunities here, the first is to introduce some way of typing a function to simply differentiate it from an object at compile time.  The second would be to make it possible to indicate the type of the list of arguments... so that you could use them elsewhere.  For instance, the glue method could end up looking like:

public static function glue( func1:#A -> B, func2: #A -> C ) : #A -> B {}

You could then use it on any function, with any arity.

This is not a big problem for me, but worth considering.

Best,
Justin



On Tue, Jan 26, 2010 at 10:55 AM, Nicolas Cannasse <[hidden email]> wrote:
Juan Delgado a écrit :

Hi there,

Porting some code from AS3, I want to be able to pass any function as
a parameter. In AS3 looks like this:

function wadus(myFunc : Function) : Void;

Can't find a proper way to do this in haXe. Doing something like:

function wadus(myFunc : Void -> Void) : Void;

...limits the signature of the passed function. For a moment I thought
that Type.TFunction would do the trick, but it doesn't.

I'm going Dynamic at the moment, but sure there should be a better way
of doing this?

A function which you don't know parameters is not very useful to consider as a type.

Nicolas


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



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
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
Reply | Threaded
Open this post in threaded view
|

Re: Function type?

Heinz Hölzer-2
In reply to this post by Justin Donaldson
I also ran into this limitation and would approve this feature.

I would really suggest to add a function type like #A -> B (so a general Function could be described as #Dynamic->Dynamic), getting more errors at compilation time is always a good addition.

Another Feature would be the possibility to define optional Parameters for Function Types ( var f: String->?Int->Void ).

best,
h


Am 26.01.2010 17:54, schrieb Justin Donaldson:
Hi Nicolas,

This comes up in the reflect API though, doesn't it?  There's quite a few methods there where a function is expected, but you can pass any object and the compiler won't complain.

static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic

granted, the function would fail on run-time.  However, couldn't the compiler catch these types of errors right away?

There are a few times when I am writing functions that modify other functions, but preserve the original function's argument arity and return values.  For instance, the Glue class:
http://code.google.com/p/haxle/source/browse/trunk/src/org/sugar/sigslot/Glue.hx

To preserve type safety in this case, it's necessary to specify a different function for each arity (I usually stop at 6).  However, internally each function is identical.
If there was a way to represent a function as:  #A->B then a lot of the redundancy could be removed.  In this case, #A here parametrizes the list of argument types, and B indicates just the return type,

There are two opportunities here, the first is to introduce some way of typing a function to simply differentiate it from an object at compile time.  The second would be to make it possible to indicate the type of the list of arguments... so that you could use them elsewhere.  For instance, the glue method could end up looking like:

public static function glue( func1:#A -> B, func2: #A -> C ) : #A -> B {}

You could then use it on any function, with any arity.

This is not a big problem for me, but worth considering.

Best,
Justin


On Tue, Jan 26, 2010 at 10:55 AM, Nicolas Cannasse <[hidden email]> wrote:
Juan Delgado a écrit :

Hi there,

Porting some code from AS3, I want to be able to pass any function as
a parameter. In AS3 looks like this:

function wadus(myFunc : Function) : Void;

Can't find a proper way to do this in haXe. Doing something like:

function wadus(myFunc : Void -> Void) : Void;

...limits the signature of the passed function. For a moment I thought
that Type.TFunction would do the trick, but it doesn't.

I'm going Dynamic at the moment, but sure there should be a better way
of doing this?

A function which you don't know parameters is not very useful to consider as a type.

Nicolas


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



--
Justin Donaldson
PhD Candidate, Informatics
Indiana University
http://www.scwn.net
aim: iujjd
twitter: jjdonald


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