Haxe: Running @:build macro on typedefs

As at 3.2.1, Haxe does not support @:build macros over a typedef. But I just discovered a trick to do that. That utilizes a @:genericBuild macro. Well, honestly, this is not truly a build macro because the type is read-only. i.e. you can’t add or remove or alter fields of the original type. But you can declare a new type based on that and return it as the type for the call site. I found it quite useful for my use case because I would like to make a copy of the typedef in some fashion.

@:build(Macro.build()) // this doesn't work
typedef MyType = {}

@:genericBuild(Macro.build()) // the trick is here
class Wrapper<T> {}

In Macro.hx:

public static function build() {
    var ret = null;

    var type = switch Context.getLocalType() {
        case TInst(_, [param]):
            // here the `param` is MyType, store it and return it
            // at the end of the macro
            // so that `var t:Wrapper<MyType>` is actually 
            // equivalent to `var t:MyType`

            throw 'Invalid Wrapper';

    // at this point, do whatever you need with `MyType`, 
    // as if a build macro is running on it

    return type; // return it at last

Haxe macro: copying expressions from another Type

I was playing around inspecting the contents of other Types through haxe macro when I found that I can copy expressions from another type very easily. I think I should write it down as a note for myself.

A possibly interesting use-case is that Haxe doesn’t inline your function even you marked it as inline, if you try to store the function in a variable.

static function main() {
  var t = test; // outputs `var t = test;` instead of `var t = function() {trace("test");}`

static inline function test() trace("test");

However, with the following simple macro trick, we can assign a function to a variable “inlined”

Continue reading Haxe macro: copying expressions from another Type

Transform js-style callback to Future with Haxe macro

EDIT (4 July 2016): A newer solution for this problem can be found at: https://github.com/kevinresol/futurize

Haxe macro is very powerful and it is really good at eliminating boiler-plate codes.

In the past 2 months I have been working on a web server using haxe/nodejs + ufront + mongoose (through js-kit). Since ufront supports async operations through Futures while mongoose uses js-style function callbacks, I have to write a lot of bolier-plate codes to convert the latter to the former, like this:

However, it is very cumbersome. If I have more than 100 database call like that, I have to write that code 100 times. Continue reading Transform js-style callback to Future with Haxe macro

How to write jQuery plugins in Haxe

This blog post recorded how I came up with the idea. You may jump to the GitHub repo directly, example usage is written there.

Everthing started from an old jQuery plugin

Recently I started to use Haxe as my primary programming language. I have always been thinking about what I can do with this modern language. Until I dug out an old jQuery plugin, which I wrote several years ago, while I was cleaning up some old files in my server. I remembered that it wasn’t easy to develop in javascript. It is because at that time there was no (or at least I was not aware of any) decent IDE for javascript. Even with the powerful debugger FireBug, which just began to gain popularity at that time, I still found it painful to debug javascript programs. Therefore I have stopped writing in javascript since then. Looking at the plugin’s forgotten to-do list and the unfinished next-version, I decided to find a way writing jQuery plugins (again) in Haxe.

Here is my goal to be done in Haxe:

  1. Register a function as a jQuery plugin by adding it to the jQuery.fn object
  2. Able to access the “this” object within the plugin function as in vanilla javascript
  3. The javascript output should be as simple and clean as possible
  4. Most important: Leverage Haxe’s feature such as Macros, Code-Completion, Compiler Checks to write a decent plugin

Continue reading How to write jQuery plugins in Haxe

Haxe SPOD: Specifying a unique field

Haxe has a very nice library called SPOD Macros which allows us to manipulate database tables as strictly-typed objects instead of dealing with the plain-text SQL commands and the clumsy result sets/arrays of untyped row objects.

The Problem

As at Haxe 3.0.1, SPOD does not provide a compile-time solution (I mean metadata like @:table and @:relation) to specify a field to be unique. One of the solution is to first create the table in Haxe, then use a database management tool such as phpMyAdmin to configure the column to be unique. However, I would like to do this in my Haxe code, after all we are programmers!

Continue reading Haxe SPOD: Specifying a unique field