All posts by Kevin

A few approaches to utilize Haxe and its targets

Haxe is a programming language that can be transpiled to many other languages thanks to the powerful Haxe compiler. Because of this very nature, Haxe is really versatile and the Haxe developer spectrum is really wide. Developers use Haxe to develop games, web apps, mobile apps, desktop apps, servers, tools, etc. Therefore, when authoring a Haxe library, ideally it should cover as many targets as possible. So that everyone would be able to use it. Here I would like to list a few approaches to utilize Haxe and its targets to write truly cross-target code, using a work-in-progress Big Integer library I have been working on recently.

Make every Haxe target work for you

Continue reading A few approaches to utilize Haxe and its targets

Apply @:genericBuild on a function in Haxe

Acknowledgement: The solution about applying @:genericBuild on functions is actually inspired by the haxetink libraries, in particular tink_json.

@:genericBuild in a nutshell

In Haxe, @:genericBuild is a metadata that tags a class to be built with a macro, which is invoked for every occurrence of the class.


// ThreeDArray.hx
class ThreeDArray<T> {}

// MyMacro.hx
class MyMacro {
  public static function build() {
    switch haxe.macro.Context.getLocalType() {
      case TInst(_, [p]):
        var complexType = haxe.macro.TypeTools.toComplexType(p);
        return macro:Array<Array<Array<$complexType>>>;
      default: throw 'assert'; // technically unreachable

// Then in somewhere:
var my3dIntArray:ThreeDArray<Int>;
var my3dStringArray:ThreeDArray<String>;
// etc...

Continue reading Apply @:genericBuild on a function in Haxe

Method Overloading achieved with Abstracts in Haxe

Method Overloading in general

In this post I am going to introduce “method overloading” in the Haxe programming language. In general, method overloading means that a collection of functions with different type signatures, that share the same name. For example in Java, one would write the following code:

public void foo(String bar) { /* body ... */ }
public void foo(String[] bars) { /* body ... */ }

Then you can call either foo("my string") or foo(new String[]{"my", "string"}).

method overloading with haxe abstracts

Continue reading Method Overloading achieved with Abstracts in Haxe

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.

Continue reading Haxe: Running @:build macro on typedefs

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:

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