Skip to content

Instantly share code, notes, and snippets.

View nadako's full-sized avatar

Dan Korostelev nadako

View GitHub Profile
@nadako
nadako / ArrayRead.hx
Last active March 14, 2024 07:57
Recursive read-only type generator for JSON structures (based on @:genericBuild)
abstract ArrayRead<T>(Array<T>) from Array<T> {
@:arrayAccess inline function get(i:Int):T return this[i];
public var length(get,never):Int;
inline function get_length() return this.length;
}
using Tools;
class Main {
static function main() {
var a = haxe.ds.Option.Some(42);
var s = a.extract(Some(v) => {value: v});
trace(s);
}
}
import haxe.Constraints.Function;
@:pythonImport("flask", "Flask")
extern class Flask {
function new(module:String);
function run():Void;
function route<T:Function>(path:String):T->T;
}
class Main {
@nadako
nadako / 1 Main.hx
Last active October 16, 2021 04:38
ocaml-like `with` for Haxe
using WithMacro;
typedef Player = {
final name:String;
final level:Int;
}
class Main {
static function main() {
var player = {name: "Dan", level: 15};
@nadako
nadako / Main.hx
Created June 24, 2015 19:36
Haxe + SDL = native love \o/
class Main {
static function main() {
Sdl.init(Sdl.INIT_EVERYTHING);
var win = Sdl.createWindow("Hello", 100, 100, 800, 600, Sdl.WINDOW_OPENGL);
var ren = Sdl.createRenderer(win, -1, Sdl.RENDERER_ACCELERATED);
var bmp = Sdl.loadBMP("test.bmp");
var tex = Sdl.createTextureFromSurface(ren, bmp);
Sdl.freeSurface(bmp);
for (i in 0...3) {
@nadako
nadako / Globals.hx
Last active February 7, 2021 06:43
A small hack to make JS global variable easily available in Haxe
extern class Globals
{
public static var context(get, never):Context;
inline static function get_context():Context return untyped __js__("context");
}
extern class Context
{
public function doStuff():Void;
}
@nadako
nadako / Main.hx
Last active November 27, 2020 16:08
Signal builder using new Rest type parameter in Haxe
class Main {
static function main() {
var signal = new Signal<Int,String>();
var conn = signal.connect(function(a, b) {
trace('Well done $a $b');
});
signal.dispatch(10, "lol");
}
}
@nadako
nadako / SchemaTypeBuilder.hx
Last active October 31, 2020 03:34
JSON-schema type builder prototype.
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
class SchemaTypeBuilder
{
public static function build(ref:String):haxe.macro.Type
{
var schema = haxe.Json.parse(sys.io.File.getContent(ref));
var type:ComplexType = parseType(schema);
@nadako
nadako / Main.hx
Created March 18, 2020 15:59
declaration-site unification check
class Point implements UnifyCheck<{x:Int}> {
public var x:Int;
}
class Main {
static function main() {
}
}

--server-connect

Haxe 4.0 introduces a new way of communicating IDE and the compiler.

Here's what an IDE needs to do:

  • start a TCP server on some port (can pass 0 to bind to any available one)
  • start haxe --server-connect <port> where <port> is replaced with the port number the started TCP server was bound to.
  • haxe will connect to that server and await requests, ensure that happens
  • send messages of the following structure: