Making my own data types

68 views
Skip to first unread message

Sami Habib

unread,
Aug 2, 2015, 9:17:56 AM8/2/15
to Haxe
Is it possible to somehow create my own data types? For example, say I wanted to make UInt8 that can only hold 8-bit positive integers, from 0 to 255, which wraps around if it goes over. Could I somehow extend Int so that it does "& 0xFF" with any value it's given?

I understand that I would be able to make some class and make stuff like setters and getters, but it would be nice to have a proper data type that I can just run operators on directly, like Int.

Can I do this, and if so how?

Justin L Mills

unread,
Aug 2, 2015, 9:40:54 AM8/2/15
to haxe...@googlegroups.com
You need to research abstracts
http://haxe.org/manual/types-abstract.html

> run operators on directly, like Int.
http://haxe.org/manual/types-abstract-operator-overloading.html

Just be mindful of checking what the abstract converts to in terms of
inline functions etc... in your target, and don't expect perfection
abstracts can be a bit weird.


Best Justin




On 02/08/2015 14:17, Sami Habib wrote:
> Is it possible to somehow create my own data types? For example, say I
> wanted to make UInt8 that can only hold 8-bit positive integers, from
> 0 to 255, which wraps around if it goes over. Could I somehow extend
> Int so that it does "& 0xFF" with any value it's given?
>
> I understand that I would be able to make some class and make stuff
> like setters and getters, but it would be nice to have a proper data
> type that I can just
>
> Can I do this, and if so how?
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google
> Groups "Haxe" group.
> For more options, visit https://groups.google.com/d/optout.

Sami Habib

unread,
Aug 2, 2015, 10:04:39 AM8/2/15
to Haxe
Thanks, that's exactly what I was looking for :) I'm wondering though, is it necessary to overload existing operators? In my previous example I would expect so (to make sure the value wraps around), but what if I made a type of Int that added and subtracted in the same way? Would I need to make addition and subtraction functions?

Justin L Mills

unread,
Aug 2, 2015, 10:28:51 AM8/2/15
to haxe...@googlegroups.com
much of abstract is a compile feature it magic's away in the target runtime, but if you need your type to stay as your type then
http://haxe.org/manual/types-abstract-implicit-casts.html
may not work very well...
myInt1 + myInt2 = anInt;
anInt.isPrime(); // fails to call an isPrime function on MyInt abstract.
So it really depends if you want an operation to be able to result in your type unless you want to cast it back
var i: MyInt = myInt1 + myInt2;
i.isPrime();
Have a play it's the best way to get a feel for what you can and can't do but often you need to not rely on haxe's implicit typing, as your type under operations could easily end up back as an Int.

Justin L Mills

unread,
Aug 2, 2015, 10:43:01 AM8/2/15
to haxe...@googlegroups.com
correction
much of abstract is a compile feature it magic's away in the target runtime, but if you need your type to stay as your type then
http://haxe.org/manual/types-abstract-implicit-casts.html

var anInt = myInt1 + myInt2;
anInt.isPrime();// fails as the two MyInt when from and to on an abstact without operations defined will result in the Int.

but

var anInt: MyInt = ( myInt1 + myInt2 );
anInt.isPrime();

may work, but sometimes you need to do it in two steps.

var anInt1 = myInt1 + myInt2; //Int
var anInt2: MyInt = anInt1; // converted to MyInt
anInt2.isPrime();

best to experiment to get a true feel and maybe check what it actually compiles to since it maybe adding some inline function overheads that you can't justify other times it may not matter.

Luca Deltodesco

unread,
Aug 2, 2015, 12:30:17 PM8/2/15
to Haxe
JLM, that's not quite correct, even if MyInt is 'from Int to Int' the compiler will still not let you do MyInt + MyInt automatically as it generally only allows a single implicit cast, but here it would require 2 for both sides of the +. You can try that yourself with abstract A(Int) from Int to Int {} trying to add two A will give 'cannot add A and A'

Sami Habib, abstracts are the correct answer here though, and yes you'd define the +/- operators to do what you wanted

Justin L Mills

unread,
Aug 2, 2015, 2:14:39 PM8/2/15
to haxe...@googlegroups.com
Ah sorry, yep should follow my own advise "best to experiment".

In this case casting works and does not seem to add much in terms of
overhead on js target atleast ( see js source ).

var tot:MyInt = ( cast myInt0 ) + ( cast myInt1 );
trace( tot.isFive() );

"best to experiment" - see details here:

http://try.haxe.org/#862D6

Justin L Mills

unread,
Aug 2, 2015, 2:16:08 PM8/2/15
to haxe...@googlegroups.com
I guess that's not really typed though... so kind of illegal if you
don't want nasty to locate bugs.

Luca Deltodesco

unread,
Aug 2, 2015, 4:11:54 PM8/2/15
to Haxe
it'd be much cleaner to just define a + operator on MyInt instead of casting, but if you 'are' going to cast, atleast do it like (myInt0 : Int) + (myInt1 : Int) to make it explicit..
Reply all
Reply to author
Forward
0 new messages