extending core classes

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

extending core classes

Joshua Harlan Lifton
I'd like to extend some core classes, such as Math (add
arithmeticModulo and constrain functions) and DateTools (override
format to properly handle locale-dependent format parameters for my
locale). I see there might be some Flash player problems with doing
something like this:

http://lists.motion-twin.com/pipermail/haxe/2006-August/004452.html

Furthermore, I want to change minimally code I already have in place,
so I'd like the extended classes to be the same names as the core
classes. Unfortunately, something like:

package foo.bar.utility;
class Math extends Math {
...
}

causes a "Recursive class" compilation error.

Is it possible to get around either of these limitations?

Thanks,
Josh

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

Re: extending core classes

Franco Ponticelli
You can locally replace those type definitions inside your project or library (modules with the same name/path are taken first from the project and after from std) or you can create you utility classes which is a better option in my opinion.
About Dates I've added a few interesting functions in thx (http://code.google.com/p/thx-core/) once I merge hxculture into that project.

Franco

On Wed, Apr 20, 2011 at 12:25 PM, Joshua Harlan Lifton <[hidden email]> wrote:
I'd like to extend some core classes, such as Math (add
arithmeticModulo and constrain functions) and DateTools (override
format to properly handle locale-dependent format parameters for my
locale). I see there might be some Flash player problems with doing
something like this:

http://lists.motion-twin.com/pipermail/haxe/2006-August/004452.html

Furthermore, I want to change minimally code I already have in place,
so I'd like the extended classes to be the same names as the core
classes. Unfortunately, something like:

package foo.bar.utility;
class Math extends Math {
...
}

causes a "Recursive class" compilation error.

Is it possible to get around either of these limitations?

Thanks,
Josh

--
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: extending core classes

justin_mills
In reply to this post by Joshua Harlan Lifton
>From a flash player perspective you are not ment to be able to extend Math class, although it maybe possible to hack access to its prototype at run time and I am sure you can add to it some how with haXe powers but I don't really think it is a good idea, it's a well documented class and any changes made should be for the whole haXe community otherwise any code you write is going to be difficult for another person to fault find, but I don't really see why your not happy with creating a new class, it seems like the more reliable approach, changing the Math class would effect other movies loaded in because it sits outside the applicationdomain.  I think there are a number of reasons for not doing this on the flashplatform and possibly on others. But maybe others have different opinions - the link you posted was in relation to flash so ignore my comments if it's another target your talking about.



 On Wed 20/04/11 13:25, Joshua Harlan Lifton [hidden email] wrote:

> I'd like to extend some core classes, such as Math (add
> arithmeticModulo and constrain functions) and DateTools (override
> format to properly handle locale-dependent format parameters for my
> locale). I see there might be some Flash player problems with doing
> something like this:
>
> http://lists.motion-twin.com/pipermail/haxe/2006-August/004452.
> html
> Furthermore, I want to change minimally code I already have in place,
> so I'd like the extended classes to be the same names as the core
> classes. Unfortunately, something like:
>
> package foo.bar.utility;
> class Math extends Math {
> ...
> }
>
> causes a "Recursive class" compilation error.
>
> Is it possible to get around either of these limitations?
>
> Thanks,
> Josh
>
> --
> 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: extending core classes

Joshua Harlan Lifton
I'm not looking to changing the core Math class, just extend it so the
extension can serve as a drop in replacement for the current Math
class. In other words, I just want to be able to add "import
foo.bar.Math;" and have all my existing code work as it already does,
with the additional benefit of having new methods at my disposal, such
as arithmeticModulo and constrain.

On Wed, Apr 20, 2011 at 7:56 AM,  <[hidden email]> wrote:

> >From a flash player perspective you are not ment to be able to extend Math class, although it maybe possible to hack access to its prototype at run time and I am sure you can add to it some how with haXe powers but I don't really think it is a good idea, it's a well documented class and any changes made should be for the whole haXe community otherwise any code you write is going to be difficult for another person to fault find, but I don't really see why your not happy with creating a new class, it seems like the more reliable approach, changing the Math class would effect other movies loaded in because it sits outside the applicationdomain.  I think there are a number of reasons for not doing this on the flashplatform and possibly on others. But maybe others have different opinions - the link you posted was in relation to flash so ignore my comments if it's another target your talking about.
>
>
>
>  On Wed 20/04/11 13:25, Joshua Harlan Lifton [hidden email] wrote:
>> I'd like to extend some core classes, such as Math (add
>> arithmeticModulo and constrain functions) and DateTools (override
>> format to properly handle locale-dependent format parameters for my
>> locale). I see there might be some Flash player problems with doing
>> something like this:
>>
>> http://lists.motion-twin.com/pipermail/haxe/2006-August/004452.
>> html
>> Furthermore, I want to change minimally code I already have in place,
>> so I'd like the extended classes to be the same names as the core
>> classes. Unfortunately, something like:
>>
>> package foo.bar.utility;
>> class Math extends Math {
>> ...
>> }
>>
>> causes a "Recursive class" compilation error.
>>
>> Is it possible to get around either of these limitations?
>>
>> Thanks,
>> Josh
>>
>> --
>> 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: extending core classes

Heinz Hölzer-2
you cannot inherit static methods, so this isn't possible.

however what you can is to create your own class and create inline
functions that calls the native ones without loosing performance.

class MyMath {

     public static inline function abs (x:Float):Float
     {
         return Math.abs();
     }
     ...
     public static inline function yourOwn(x:Float):Float
}

Am 20.04.2011 14:55, schrieb Joshua Harlan Lifton:

> I'm not looking to changing the core Math class, just extend it so the
> extension can serve as a drop in replacement for the current Math
> class. In other words, I just want to be able to add "import
> foo.bar.Math;" and have all my existing code work as it already does,
> with the additional benefit of having new methods at my disposal, such
> as arithmeticModulo and constrain.
>
> On Wed, Apr 20, 2011 at 7:56 AM,<[hidden email]>  wrote:
>> > From a flash player perspective you are not ment to be able to extend Math class, although it maybe possible to hack access to its prototype at run time and I am sure you can add to it some how with haXe powers but I don't really think it is a good idea, it's a well documented class and any changes made should be for the whole haXe community otherwise any code you write is going to be difficult for another person to fault find, but I don't really see why your not happy with creating a new class, it seems like the more reliable approach, changing the Math class would effect other movies loaded in because it sits outside the applicationdomain.  I think there are a number of reasons for not doing this on the flashplatform and possibly on others. But maybe others have different opinions - the link you posted was in relation to flash so ignore my comments if it's another target your talking about.
>>
>>
>>
>>   On Wed 20/04/11 13:25, Joshua Harlan Lifton [hidden email] wrote:
>>> I'd like to extend some core classes, such as Math (add
>>> arithmeticModulo and constrain functions) and DateTools (override
>>> format to properly handle locale-dependent format parameters for my
>>> locale). I see there might be some Flash player problems with doing
>>> something like this:
>>>
>>> http://lists.motion-twin.com/pipermail/haxe/2006-August/004452.
>>> html
>>> Furthermore, I want to change minimally code I already have in place,
>>> so I'd like the extended classes to be the same names as the core
>>> classes. Unfortunately, something like:
>>>
>>> package foo.bar.utility;
>>> class Math extends Math {
>>> ...
>>> }
>>>
>>> causes a "Recursive class" compilation error.
>>>
>>> Is it possible to get around either of these limitations?
>>>
>>> Thanks,
>>> Josh
>>>
>>> --
>>> 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: extending core classes

Joshua Harlan Lifton
Ah, right. I'd forgotten about the lack of static method inheritance
and that all those methods are static. I'll just stick with importing
foo.bar.ExtraMath for now.

Thanks,
Josh

2011/4/20 Heinz Hölzer <[hidden email]>:

> you cannot inherit static methods, so this isn't possible.
>
> however what you can is to create your own class and create inline functions
> that calls the native ones without loosing performance.
>
> class MyMath {
>
>    public static inline function abs (x:Float):Float
>    {
>        return Math.abs();
>    }
>    ...
>    public static inline function yourOwn(x:Float):Float
> }
>
> Am 20.04.2011 14:55, schrieb Joshua Harlan Lifton:
>>
>> I'm not looking to changing the core Math class, just extend it so the
>> extension can serve as a drop in replacement for the current Math
>> class. In other words, I just want to be able to add "import
>> foo.bar.Math;" and have all my existing code work as it already does,
>> with the additional benefit of having new methods at my disposal, such
>> as arithmeticModulo and constrain.
>>
>> On Wed, Apr 20, 2011 at 7:56 AM,<[hidden email]>  wrote:
>>>
>>> > From a flash player perspective you are not ment to be able to extend
>>> > Math class, although it maybe possible to hack access to its prototype at
>>> > run time and I am sure you can add to it some how with haXe powers but I
>>> > don't really think it is a good idea, it's a well documented class and any
>>> > changes made should be for the whole haXe community otherwise any code you
>>> > write is going to be difficult for another person to fault find, but I don't
>>> > really see why your not happy with creating a new class, it seems like the
>>> > more reliable approach, changing the Math class would effect other movies
>>> > loaded in because it sits outside the applicationdomain.  I think there are
>>> > a number of reasons for not doing this on the flashplatform and possibly on
>>> > others. But maybe others have different opinions - the link you posted was
>>> > in relation to flash so ignore my comments if it's another target your
>>> > talking about.
>>>
>>>
>>>
>>>  On Wed 20/04/11 13:25, Joshua Harlan Lifton
>>> [hidden email] wrote:
>>>>
>>>> I'd like to extend some core classes, such as Math (add
>>>> arithmeticModulo and constrain functions) and DateTools (override
>>>> format to properly handle locale-dependent format parameters for my
>>>> locale). I see there might be some Flash player problems with doing
>>>> something like this:
>>>>
>>>> http://lists.motion-twin.com/pipermail/haxe/2006-August/004452.
>>>> html
>>>> Furthermore, I want to change minimally code I already have in place,
>>>> so I'd like the extended classes to be the same names as the core
>>>> classes. Unfortunately, something like:
>>>>
>>>> package foo.bar.utility;
>>>> class Math extends Math {
>>>> ...
>>>> }
>>>>
>>>> causes a "Recursive class" compilation error.
>>>>
>>>> Is it possible to get around either of these limitations?
>>>>
>>>> Thanks,
>>>> Josh
>>>>
>>>> --
>>>> 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