Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Last active March 24, 2025 06:15
Show Gist options
  • Save diegopacheco/c33ef1d8e41ba376e240358ce78ebd3d to your computer and use it in GitHub Desktop.
Save diegopacheco/c33ef1d8e41ba376e240358ce78ebd3d to your computer and use it in GitHub Desktop.
Haxe Lang Tiny Essay

Haxe Lang Tiny Essay

created: 23.MAR.2025

I love to learn new programing languages, it help to open the mind to new possibilities and compare different approaches. For instance, I learned Ruby and Scala in 2010, Clojure and Haskell in 2011, Go in 2015, Kotlin 2016, Rust in 2018 and Idris, TypeScript in 2019, 2020 Pandemic strike did a bunch of pocs but not with new langs(crazy year), Zig in 2021, 2022(coding in lots of langs but nothing new) - in 2023 I'm learning Nim and V. Learn at least one lang per year. This post is not complain, it's just to share some toughts, notes and impressions.

Why Haxe

  • Multi-plataform
  • Good for Games
  • High-level strictly-typed
  • Can build cross-platform applications targeting JS, C++, C#, Java, JVM, Python, Lua, PHP, Flash

My Feelings (12-Jan-2025 haxe 4.3.6)

  • Cool
  • Fun
  • Syntax feels like JavaScript
  • You will see slighly different outputs depending on the target language

Show me the code

My POCs with Haxe: https://github.com/diegopacheco/haxe-playground

1 - OOP Support

Haxe has classes, constructors and several OOP features like Java.

class Point {
    var x:Int;
    var y:Int;
  
    public function new(x, y) {
      this.x = x;
      this.y = y;
    }
  
    public function toString() {
      return "Point(" + x + "," + y + ")";
    }
}

class Main {
    static public function main() {
        var p = new Point(10, 20);
        trace(p.toString());
    }
}

2 - Generics

Like Java, Scala or Kotlin, Haxe has support for generics.

@:generic
class MyValue<T> {
  public var value:T;

  public function new(value:T) {
    this.value = value;
  }
}

class Main {
  static public function main() {
    var a = new MyValue<String>("Hello");
    trace(a);

    var b = new MyValue<Int>(42);
    trace(b);
  }
}

3 - Pattern Matcher

Like Haskell, Scala or Rust - Haxe has Pattern matcher.

class Main {
    static public function main() {
        var myArray = [7, 6];
        var s = switch (myArray) {
            case [a, b] if (b > a):
                b + ">" + a;
            case [a, b]:
                b + "<=" + a;
            case _: "found something else";
        }
        trace(s); // 6<=7
    }
}

4 - Maps

Like any decent language, there are collections, here is how we use Maps.

class Main {
    static public function main() {
        var capitals = ["Brazil" => "Brasilia", "USA" => "Washington", "Argentina" => "Buenos Aires"];
        trace(capitals);
        trace(capitals["Brazil"]);
        for (country in capitals.keys()) {
            trace(country + " => " + capitals[country]);
        }
    }
}

5 - STD Lib support for Unit Tests

I love languages that have good STD libs so it minimize how many external libs you need to use. Haxe has Unit Tests support, built-in, IMHO a bit verbose but still cool.

import utest.Runner;
import utest.ui.Report;
import utest.Test;
import utest.Assert;

class MyTestCase extends Test {
    public function testBasic() {
        Assert.equals("A", "A");
    }
}

class Main {
  public static function main() {
    //the long way
    var runner = new Runner();
    runner.addCase(new MyTestCase());
    Report.create(runner);
    runner.run();
    //the short way in case you don't need to handle any specifics
    utest.UTest.run([new MyTestCase()]);
  }
}

6 - Operator Overloading

Haxe support Operator overloading like Scala.

abstract MyAbstract(String) {
    public inline function new(s:String) {
      this = s;
    }
  
    @:op(A * B)
    public function repeat(rhs:Int):MyAbstract {
      var s:StringBuf = new StringBuf();
      for (i in 0...rhs)
        s.add(this);
      return new MyAbstract(s.toString());
    }
  }
  
  class Main {
    static public function main() {
      var a = new MyAbstract("foo");
      trace(a * 3); // foofoofoo
    }
  }

7 - Building to Target Langs

Haxe allow you to build to several target langs such as JS, C++, C#, Java, JVM, Python, Lua, PHP, Flash. Here are some examples

Java

haxe --main Main --jvm Main.jar

JS

haxe --main Main --js Main.js

Python

haxe --main Main --python main.py

Other Tiny Essays

About me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment