Skip to content

Instantly share code, notes, and snippets.

@back2dos
back2dos / DevTools.hx
Last active January 2, 2016 09:39
Measure execution time with macros.
package ;
class DevTools {
macro static public function measure(e, ?msg:String) {
if (msg == null)
msg = haxe.macro.ExprTools.toString(e);
return macro @:pos(e.pos) {
var start = haxe.Timer.stamp(),
result = [$e];//wrapping in the array skews the result a bit, but avoids issues with Void
var duration = haxe.Timer.stamp() - start;
@back2dos
back2dos / Foo.hx
Last active December 31, 2015 22:59
Stringly fail.
package ;
#if macro
import haxe.macro.Expr;
import haxe.macro.Context;
using haxe.macro.ExprTools;
#end
class Foo {
public function new() {
#if macro
import haxe.macro.Expr;
#else
class Image {
static public function img_tag(rr:RR, image: {path:String, w:Int, h:Int}, target_size):String {
return '<img alt="whoops">';
}
}
#end
typedef RR = Dynamic;
@back2dos
back2dos / Main.hx
Last active December 24, 2015 21:19
Promising benchmarks. Surprising results.
package ;
import tink.core.Future;
using tink.core.Outcome;
import haxe.Timer.*;
class Main {
macro static function benchmark(name:String, makeTrigger, getFuture, watch, trigger)
return macro {
name: $v{name},
@back2dos
back2dos / Signal.hx
Last active December 23, 2015 09:19
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
#end
abstract Signal<T>(Array<T>)
{
public inline function new()
{
this = [];
@back2dos
back2dos / Main.hx
Created September 13, 2013 11:50
Fun with chains.
package ;
using Main;
typedef Future<T> = (T->Void)->Void;
typedef Chain<T> = Future<Option<{ data: T, next: Chain<T>}>>;
class Main {
static function lazy<A>(g:Void->A) {
var done = false,
@back2dos
back2dos / Module.hx
Last active December 22, 2015 11:19
Compile only classes belonging to a given module
package ;
import haxe.macro.Type;
import haxe.macro.Expr;
import haxe.macro.Context;
class Module {
static function build(?module:String) {
function isIncluded(meta:MetaAccess)
@back2dos
back2dos / example.js
Last active December 21, 2015 22:39
Scoped routers.
function wrapRouter(addRoute, basicFunctionality) {
return {
register: function (rule, handler) {
require("util").inherits(handler, basicFunctionality);
var factory = function (req, res) {
this.request = req;
this.response = res;
};
factory.prototype = handler;
addRoute(rule, function (req, res) {
package ;
import Map;
import cpp.vm.Mutex;
abstract ThreadSafeMap<K, V>({ data: IMap<K, V>, lock: Mutex }) {
public function new(data:IMap<K, V>)
this = { data: data, lock: new Mutex() }
@back2dos
back2dos / Main.hx
Last active December 20, 2015 12:09
package ;
class Main {
static function main() test(Foo);
static function test(e:MyEnum) {}
static function test2():MyEnum
return Foo;
}