views:

155

answers:

3

I'm working on some ActionScript code that needs to juggle a bunch of similar-but-not-interchangeable types (eg, position-in-pixels, internal-position, row-and-column-position) and I'm trying to come up with a naming scheme to minimize the complexity.

Additionally, I don't yet know what the best format for the "internal position" is – using int, uint and Number all have advantages and disadvantages.

Normally I'd solve this with a typedef:

typedef float pixelPos;
typedef int internalPos;
typedef int rowColPos;

Is there any way of getting similar functionality in ActionScript?

A: 

I have found an article titled Typedefs in ActionScript 3, which suggests using:

const pixelPos:Class = int;

But that doesn't work – the compiler complains that "Type was not found or was not a compile-time constant: pixelPos" (note: this also happens when I use Object instead of int).

Here is an example of code which doesn't compile:

const pixelPos:Class = int;
function add3(p:pixelPos):void { // <-- type not found on this line
    return p + 3;
}
David Wolever
A: 

If you're using Flex or another command-line compiler to build your project, you could add a pass from an external preprocessor to your build process.

Doesn't get the type-safety, but otherwise appears to do what you want.

Anon.
Eventually I might move to building my project out side of FlexBuilder… But I don't quite want to take that leap yet – last time I looked (about six months ago), I had decided that it was going to be more work than it was worth.
David Wolever
I haven't used Flex Builder - but as far as I know it just calls the command-line compiler with the right arguments. You should, then, be able to insert a call to something else by tweaking some settings.
Anon.
Ha, yes, see, you're assuming that the people who built FlexBuilder were sensible… Which turns out to be a faulty assumption. I've spent a fair amount of time looking, and as far as I can tell it's impossible :(
David Wolever
A: 

I've also tried using:

mxmlc -define+=TYPES::pixelPos,int ...

But, of course, that doesn't work either (the compiler expects that -defined things are compile-time constants, not arbitrary "stuff to replace other stuff with", like a sensible compiler would).

David Wolever