Add ComplexType::intersect.

This commit is contained in:
Juraj Kirchheim
2018-09-01 15:09:05 +02:00
parent 9bcec770eb
commit 682aeacce8

View File

@@ -7,9 +7,7 @@ import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using tink.macro.Exprs;
using tink.macro.Positions;
using tink.macro.Functions;
using tink.MacroApi;
using tink.CoreApi;
class Types {
@@ -183,6 +181,33 @@ class Types {
return ret;
}
static public function intersect(types:Array<ComplexType>, ?pos:Position):Outcome<ComplexType, Error> {
if (types.length == 1) return Success(types[1]);
var paths = [],
fields = [];
for (t in types)
switch t {
case TPath(p): paths.push(p);
case TAnonymous(f):
for (f in fields) fields.push(f);
case TExtend(p, f):
for (f in fields) fields.push(f);
for (p in paths) paths.push(p);
default:
return Failure(new Error(t.toString() + ' cannot be interesected', pos));
}
return Success(TExtend(paths, fields));
}
static public function lazyComplex(f:Void->Type)
return
TPath({