@:coreType abstract XCoord from Float { }
@:coreType abstract YCoord from Float { }
One "virtual x" corresponds to 4 "physical pixels", and one "virtual y" corresponds to 8 "physical pixels".
--
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.
To unsubscribe from this group and stop receiving emails from it, send an email to haxelang+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Hi!
I'm curious about what the practical uses of abstract types can be (http://haxe.org/manual/abstracts), and how it could affect my solutions.
Two examples here:
1.
In my music notation solution, I use "virtual coordinates" for calculation of distances and measurements. These virtual x- and y-coordinates have different "physical" units:
One "virtual x" corresponds to 8 "physical pixels", and one "virtual y" corresponds to 8 "physical pixels".
Today, these virtual coordinates are represented by Float types.
It would be very elegant if these different virtual types could be represented by abstract types, wouldn't it? Let's say:
@:coreType abstract XCoord from Float { }@:coreType abstract YCoord from Float { }
This way the code would be both safer and easier to read...
Proper use of abstract type?
2.
A file path is a common unit in programming, with some clearly defined attributes: One or many segments separated by predefined types of separators, could start with a physical drive letter etc...
Would it be possible to define a Filepath as an abstract type, in a way that it could itself keep track of it's validity?
Proper use of abstract type?
--
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.
To unsubscribe from this group and stop receiving emails from it, send an email to haxelang+unsubscribe@googlegroups.com.
You should not use @:coreType, but instead define Float as the underlying type:
abstract XCoord(Float) { ... }
You can then go ahead and allow constrained arithmetic operators, e.g.:
@:op(A+B) static public function add(lhs:XCoord, rhs:XCoord):XCoord; // no implementation
This allows you to add two XCoord objects, but complains if you try XCoord + YCoord.
You could also define an implicit cast from XCoord to YCoord:
@:to public inline function toYCoord():YCoord return new YCoord(this * 2);
That's a nice explanation of boring old Java abstract classes, but the haxe abstract type is something quite different. ;) You can read an introduction here: http://haxe.org/manual/abstracts
That's a nice explanation of boring old Java abstract classes, but the haxe abstract type is something quite different. ;) You can read an introduction here: http://haxe.org/manual/abstracts
Are there plans to offer boring old Java abstract classes as a feature? Or is there a way to reproduce this behavior with existing syntax?
Well these classes are really boring, and I'm sure *sunglasses* you can use a macro for that.
So, where to draw the line where use becomes misuse...?
A few of your examples involve "not allowing" something, which I don't think is a good way of using this behavior. E.g., if file path is invalid, what would toString() return? You'd have to return a special result, and check for it... it doesn't seem to give you much benefit.
--
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.
To unsubscribe from this group and stop receiving emails from it, send an email to haxelang+u...@googlegroups.com.
Well these classes are really boring, and I'm sure *sunglasses* you can use a macro for that.
Maybe you can help me rewire my brain then... let me give a specific example.
I'm looking at a set of color classes. Colors are awkward in software, because everything has to be represented as RGB eventually. However, it's not a good inheritance situation, since you don't want to inherit internal data storage fields (R, G, B fields or arrays). I.e. you just want to make sure that a given type can generate a RGB instance somehow, and then define a whole bunch of functionality on top of that. For the given example, a new color type (HSL, HSV, CMYK, etc.) only has to override the toRGBX() and the clone() methods.
--
Oh, seems mine is a little bit more complete than Simon's :) , which:
- Simon's need to put @:abstract in methods, mine detects method without body.
- Mine allows extending an abstract class and still being abstract (let further subclass implements).
Anyway, it is rather easy to implement one using macros.
--
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.
To unsubscribe from this group and stop receiving emails from it, send an email to haxelang+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Thank you for input, Justin!Hm, I think this "not allowing" is exactly why the abstract types attract me!
A few of your examples involve "not allowing" something, which I don't think is a good way of using this behavior. E.g., if file path is invalid, what would toString() return? You'd have to return a special result, and check for it... it doesn't seem to give you much benefit.
The Int type could never hold 123.45, it does not allow it!
The Email type could never hold "Jonas Nyström", it does not allow it.
Thus, in the file path case, an invalid file path should never exist - no toString() return problem!
In my world, this would "abstract away" some of the most common problem sources in my applications.
Knowing that I can trust the cornerstone data to be nothing but absolutely valid (accordings to the type rules) would be great!
It would lower the validation DRY factor a lot!
Well, I'm thinking loud here...
It will be very exciting to see in what ways the Haxe 3 abstract type will change our solutions!
--
I still don't see how the type checking will catch every case, it seems that the run time will have to catch certain errors. E.g. what would happen with an invalid directory here:var abstract_directory:FilePath = "/some/invalid/directory/test.txt"
uh, what about haxe 2, at least?
--
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.
To unsubscribe from this group and stop receiving emails from it, send an email to haxelang+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.