Haxe: C-style enum macros

I've recently stumbled upon this little macro by Justo Delgado. It takes an "enum abstract" and sets the values of it's constant-fields incrementally. This brings up an interesting point.

See, in C, you can leave out the values and have them assigned automatically (0, 1, 2, ...), set the values manually, or mix the two, having the values continue "upwards" from the last specified value. So, for example, if I have this,

enum class Op {
	Add = 0x90, Sub,
	Mul = 0xA0, Div, Mod,
};

Op::Div will be equal to 0xA1, which is convenient, and allows grouping enum constants together (in this case storing operator priority in the upper 4 bits).

Haxe doesn't have that "out of box", unfortunately - either you make an actual enum and have the values assigned automatically, or make an @:enum abstract and assign the values manually.

But, of course, that can be fixed with a macro.

Continue reading

Haxe: Shorthand expression matching

This is a small post about a small, useful macro for Haxe.

As you may (or may not) know, Haxe has fancy enums (which are actually algebraic data types). That's nice. And you can do all sorts of fancy value matching on them, which is also nice.

What is less nice, however, is that the only conventional way to extract enum arguments is to "match" it, and thus in some situations you can only do code like this,

var r = switch (expr) {
    case one(k): k;
    default: -1;
};

adding a couple lines of code in place of what feels like it could have been a single operator.

So I wrote a tiny macro which adds just that:

var r = ematch(expr, some(k), k, -1);
Continue reading

Adding hotkeys to switch to N-th screen on Windows

Keyboard shortcuts for switching between desktops on Windows

For quite a while, the multi-screen setup on my desktop was powered by Synergy.
Which, I should say, is pretty neat, aside of it's complete unwillingness to send non-English keystrokes to additional devices.

Slightly more recently I've switched to using a more conventional dual-screen setup.
Which, of course, is more convenient (being able to drag a window to the second screen without having to sync the related media first), one thing would seem to be missing — the "hotkeys".

Moving the mouse over an entire monitor (or two) only to click something and move it back is not all that exciting, and Synergy's keyboard shortcuts for moving the mouse to N-th screen (while remembering the old position for returning) were a welcome feature.

Windows, unfortunately, does not seem to have any "built-in" keyboard shortcuts for swithing to a given screen, but that can be easily fixed with help of an AutoIt script:

Continue reading