Skip to content

Instantly share code, notes, and snippets.

@benmerckx
Created March 13, 2020 19:25
Show Gist options
  • Save benmerckx/7f453f9d944763de457c60cd20ac5b92 to your computer and use it in GitHub Desktop.
Save benmerckx/7f453f9d944763de457c60cd20ac5b92 to your computer and use it in GitHub Desktop.
import haxe.DynamicAccess;
using Reflect;
@:forward
abstract Merge<A: {}, B: {}>(B) {
public function new(a: A, b: B) {
final res: DynamicAccess<Dynamic> = cast {};
for (field in a.fields()) res[field] = a.field(field);
for (field in b.fields()) res[field] = b.field(field);
this = cast res;
}
inline function __a(): A
return cast this;
@:op(a.b) macro static function get<A: {}, B: {}>(merge: ExprOf<Merge<A, B>>, field: String) {
return macro @:privateAccess $merge.__a().$field;
}
}
class Objects {
public static function merge<A: {}, B: {}>(a: A, b: B): Merge<A, B>
return new Merge(a, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment