haXe, Object and extension methods ("mixins")

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

haXe, Object and extension methods ("mixins")

David Arno

Hi all,

 

I am looking at the “using” keyword and what I’d like to do is add a method f() to all classes in haXe. However I’m puzzled as to what the base class of all haXe classes is as it doesn’t appear to be Object. Also I’m unsure whether I can extend the base class this way. In AS3 terms, I’d assume I’d create:

 

class ObjectHelp

{

    public static function f(Object value):Object

}

 

And then by using ObjectHelp, I can do anotherInstanceOfAnyClass = instanceOfAnyClass.f()

 

So can I do this in haXe and if so, what do I use in place of Object in the above example?

 

David.


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

Re: haXe, Object and extension methods ("mixins")

laurence taylor
Dynamic works in this case, there is no root object in Haxe.

regards,
Laurence

On Mon, Dec 5, 2011 at 10:16 PM, David Arno <[hidden email]> wrote:

Hi all,

 

I am looking at the “using” keyword and what I’d like to do is add a method f() to all classes in haXe. However I’m puzzled as to what the base class of all haXe classes is as it doesn’t appear to be Object. Also I’m unsure whether I can extend the base class this way. In AS3 terms, I’d assume I’d create:

 

class ObjectHelp

{

    public static function f(Object value):Object

}

 

And then by using ObjectHelp, I can do anotherInstanceOfAnyClass = instanceOfAnyClass.f()

 

So can I do this in haXe and if so, what do I use in place of Object in the above example?

 

David.


--
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, Object and extension methods ("mixins")

Franco Ponticelli
Dynamic doesn't play well with "using" ... there is now way for the compiler to say if the method is part of the Dyanamic object or not ... you might want to try to use {} instead of Dynamic. Since creating really generic methods that apply to everything is usually not really too OOP friendly you can probably use some super class as the base type.

Franco 

On Mon, Dec 5, 2011 at 2:38 PM, Laurence Taylor <[hidden email]> wrote:
Dynamic works in this case, there is no root object in Haxe.

regards,
Laurence

On Mon, Dec 5, 2011 at 10:16 PM, David Arno <[hidden email]> wrote:

Hi all,

 

I am looking at the “using” keyword and what I’d like to do is add a method f() to all classes in haXe. However I’m puzzled as to what the base class of all haXe classes is as it doesn’t appear to be Object. Also I’m unsure whether I can extend the base class this way. In AS3 terms, I’d assume I’d create:

 

class ObjectHelp

{

    public static function f(Object value):Object

}

 

And then by using ObjectHelp, I can do anotherInstanceOfAnyClass = instanceOfAnyClass.f()

 

So can I do this in haXe and if so, what do I use in place of Object in the above example?

 

David.


--
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: haXe, Object and extension methods ("mixins")

jlm@justinfront.net
In reply to this post by David Arno
if you do using with {} approach in flash haXe your building in a lot of dependancy in the target, ie ignoring cross platform aspects, and your probably bypassing a lot of the type structuring that is important about haXe, HaXe can not really have the general inheritance structure dependancy of flash as it needs to be platform agnostic so any inheritance a platform has really you should try not to be too dependant on when designing systems.

In the haXe2 book I think there is a suggestion for an approach, but I may be wrong either way... from memory or from my own thought, basically you build a typedef that defines the type aspects your classes must have, so the compiler can then check more.

// my code may need checking...

typedef MyPoint = { var x: Int; var y: Int; }
...
class PointHelpers
{
public static function diagonalMove( point: MyPoint )
{
    point.x++;
    point.y++;
}
}
...
import PointHelpers;
using PointHelpers;
class Check extends MovieClip
{
...
var mc: MyPoint = someSprite;// may need:    cast MyPoint
mc.diagonalMove();
...
}

I may have glossed over some details ( ie you may need to set it up slightly different as your dealing with getters and setters and not just methods ), but anyway just ideas...
Reply | Threaded
Open this post in threaded view
|

Re: haXe, Object and extension methods ("mixins")

Heinz Hölzer-2
In reply to this post by David Arno
try Class<T>.


public static function f <T>(cl:Class<T>):Void {
    trace("foo");
}

Am 05.12.2011 22:16, schrieb David Arno:

Hi all,

 

I am looking at the “using” keyword and what I’d like to do is add a method f() to all classes in haXe. However I’m puzzled as to what the base class of all haXe classes is as it doesn’t appear to be Object. Also I’m unsure whether I can extend the base class this way. In AS3 terms, I’d assume I’d create:

 

class ObjectHelp

{

    public static function f(Object value):Object

}

 

And then by using ObjectHelp, I can do anotherInstanceOfAnyClass = instanceOfAnyClass.f()

 

So can I do this in haXe and if so, what do I use in place of Object in the above example?

 

David.





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

Re: haXe, Object and extension methods ("mixins")

Cauê W.
Actually it seems that the answer he wants is Dynamic.

HaXe doesn't have a base class, but Dynamic represents any type, so if you want to do anotherInstanceOfAnyClass = instance.t() with using, you can do it like this:

class UsingClass
{
public static function t(inst:Dynamic):Dynamic
{
trace("called");
return inst;
}
}

this will work, but the corollary of this is that this works on any class, enum or base type (like Int or Float)

Cheers!
Caue

2011/12/6 Heinz Hölzer <[hidden email]>
try Class<T>.


public static function f <T>(cl:Class<T>):Void {
    trace("foo");
}

Am 05.12.2011 22:16, schrieb David Arno:

Hi all,

 

I am looking at the “using” keyword and what I’d like to do is add a method f() to all classes in haXe. However I’m puzzled as to what the base class of all haXe classes is as it doesn’t appear to be Object. Also I’m unsure whether I can extend the base class this way. In AS3 terms, I’d assume I’d create:

 

class ObjectHelp

{

    public static function f(Object value):Object

}

 

And then by using ObjectHelp, I can do anotherInstanceOfAnyClass = instanceOfAnyClass.f()

 

So can I do this in haXe and if so, what do I use in place of Object in the above example?

 

David.





--
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, Object and extension methods ("mixins")

Franco Ponticelli
I've not tested it but I don't think it will work because Dynamic can have any method (and using cannot be applied correctly). On the contrary {} has no methods at all and thus it should work ;)

Franco

On Tue, Dec 6, 2011 at 7:48 AM, Cauê Waneck <[hidden email]> wrote:
Actually it seems that the answer he wants is Dynamic.

HaXe doesn't have a base class, but Dynamic represents any type, so if you want to do anotherInstanceOfAnyClass = instance.t() with using, you can do it like this:

class UsingClass
{
public static function t(inst:Dynamic):Dynamic
{
trace("called");
return inst;
}
}

this will work, but the corollary of this is that this works on any class, enum or base type (like Int or Float)

Cheers!
Caue

2011/12/6 Heinz Hölzer <[hidden email]>
try Class<T>.


public static function f <T>(cl:Class<T>):Void {
    trace("foo");
}

Am 05.12.2011 22:16, schrieb David Arno:

Hi all,

 

I am looking at the “using” keyword and what I’d like to do is add a method f() to all classes in haXe. However I’m puzzled as to what the base class of all haXe classes is as it doesn’t appear to be Object. Also I’m unsure whether I can extend the base class this way. In AS3 terms, I’d assume I’d create:

 

class ObjectHelp

{

    public static function f(Object value):Object

}

 

And then by using ObjectHelp, I can do anotherInstanceOfAnyClass = instanceOfAnyClass.f()

 

So can I do this in haXe and if so, what do I use in place of Object in the above example?

 

David.





--
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: haXe, Object and extension methods ("mixins")

Cauê W.
it works with dynamic also, stax uses it all the time, and it adds methods to any instance :)

2011/12/6 Franco Ponticelli <[hidden email]>
I've not tested it but I don't think it will work because Dynamic can have any method (and using cannot be applied correctly). On the contrary {} has no methods at all and thus it should work ;)

Franco


On Tue, Dec 6, 2011 at 7:48 AM, Cauê Waneck <[hidden email]> wrote:
Actually it seems that the answer he wants is Dynamic.

HaXe doesn't have a base class, but Dynamic represents any type, so if you want to do anotherInstanceOfAnyClass = instance.t() with using, you can do it like this:

class UsingClass
{
public static function t(inst:Dynamic):Dynamic
{
trace("called");
return inst;
}
}

this will work, but the corollary of this is that this works on any class, enum or base type (like Int or Float)

Cheers!
Caue

2011/12/6 Heinz Hölzer <[hidden email]>
try Class<T>.


public static function f <T>(cl:Class<T>):Void {
    trace("foo");
}

Am 05.12.2011 22:16, schrieb David Arno:

Hi all,

 

I am looking at the “using” keyword and what I’d like to do is add a method f() to all classes in haXe. However I’m puzzled as to what the base class of all haXe classes is as it doesn’t appear to be Object. Also I’m unsure whether I can extend the base class this way. In AS3 terms, I’d assume I’d create:

 

class ObjectHelp

{

    public static function f(Object value):Object

}

 

And then by using ObjectHelp, I can do anotherInstanceOfAnyClass = instanceOfAnyClass.f()

 

So can I do this in haXe and if so, what do I use in place of Object in the above example?

 

David.





--
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


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

Re: haXe, Object and extension methods ("mixins")

Franco Ponticelli
You are right! I was confusing the problem when the instance you want to apply the mixin to is Dynamic.

Too much work :(

Franco

On Tue, Dec 6, 2011 at 8:17 AM, Cauê Waneck <[hidden email]> wrote:
it works with dynamic also, stax uses it all the time, and it adds methods to any instance :)


2011/12/6 Franco Ponticelli <[hidden email]>
I've not tested it but I don't think it will work because Dynamic can have any method (and using cannot be applied correctly). On the contrary {} has no methods at all and thus it should work ;)

Franco


On Tue, Dec 6, 2011 at 7:48 AM, Cauê Waneck <[hidden email]> wrote:
Actually it seems that the answer he wants is Dynamic.

HaXe doesn't have a base class, but Dynamic represents any type, so if you want to do anotherInstanceOfAnyClass = instance.t() with using, you can do it like this:

class UsingClass
{
public static function t(inst:Dynamic):Dynamic
{
trace("called");
return inst;
}
}

this will work, but the corollary of this is that this works on any class, enum or base type (like Int or Float)

Cheers!
Caue

2011/12/6 Heinz Hölzer <[hidden email]>
try Class<T>.


public static function f <T>(cl:Class<T>):Void {
    trace("foo");
}

Am 05.12.2011 22:16, schrieb David Arno:

Hi all,

 

I am looking at the “using” keyword and what I’d like to do is add a method f() to all classes in haXe. However I’m puzzled as to what the base class of all haXe classes is as it doesn’t appear to be Object. Also I’m unsure whether I can extend the base class this way. In AS3 terms, I’d assume I’d create:

 

class ObjectHelp

{

    public static function f(Object value):Object

}

 

And then by using ObjectHelp, I can do anotherInstanceOfAnyClass = instanceOfAnyClass.f()

 

So can I do this in haXe and if so, what do I use in place of Object in the above example?

 

David.





--
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


--
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, Object and extension methods ("mixins")

Cauê W.

Too much work :(

I know what you mean, Franco! :) 

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

RE: haXe, Object and extension methods ("mixins")

David Arno

Thanks for all the great feedback on this one. Sounds like Dynamic may be the solution for me, but it also sounds like I might need to experiment a bit.

 

Thanks again,

David.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Cauê Waneck
Sent: 06 December 2011 15:52
To: The haXe compiler list
Subject: Re: [haXe] haXe, Object and extension methods ("mixins")

 

 

Too much work :(

 

I know what you mean, Franco! :) 


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

Re: haXe, Object and extension methods ("mixins")

Juraj Kirchheim
Technically the right way to go about this is:

class ObjectHelp {
    public static function clone<A>(value:A):A {
         return value;
    }
}

This way haXe knows that the return value is of the same type as the argument.

On Tue, Dec 6, 2011 at 4:55 PM, David Arno <[hidden email]> wrote:

> Thanks for all the great feedback on this one. Sounds like Dynamic may be
> the solution for me, but it also sounds like I might need to experiment a
> bit.
>
>
>
> Thanks again,
>
> David.
>
>
>
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Cauê Waneck
> Sent: 06 December 2011 15:52
> To: The haXe compiler list
> Subject: Re: [haXe] haXe, Object and extension methods ("mixins")
>
>
>
>
>
> Too much work :(
>
>
>
> I know what you mean, Franco! :)
>
>
> --
> haXe - an open source web programming language
> http://haxe.org

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