Tag Archives: 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: 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