到目前为止,我已经通读了:
https://haxe.org/manual/types-abstract.html
,
https://haxe.org/blog/abstracting-primitives/
,
http://old.haxe.org/manual/abstracts
https://code.haxe.org/category/other/passing-different-types-to-a-function-parameter.html
. 如果有人有其他资料可以告诉我,我会很感激的。
我基本上是在尝试创建一个接受类型为的变量的函数
String
,
Int
Float
,或
Bool
,并追踪它。不过,当我尝试编译时
haxe -main Main -cpp Export
abstracts/Comparison.hx:99: characters 42-43 : Type not found : A
. 我正试图找出为什么会发生这种情况,以及如何解决它。
我觉得我附加了太多代码,但这是我目前为止所写的:
主.hx
import abstracts.*;
class Main
{
public static function main()
{
var toPrint:Array<String> = ["String1", "String2", "String3"];
printArray(toPrint);
}
public static function printArray(toPrint:PrintableArray)
{
for (element in toPrint)
{
trace(element);
}
}
}
abstracs公司/比较.hx
package abstracts;
enum Either<A, B>
{
Left(v:A);
Right(v:B);
}
abstract Of2<A, B>(Either<A, B>) from Either<A, B> to Either<A, B>
{
@from inline static function fromA<A, B>(a:A):Of2<A, B>
{
return Left(a);
}
@from inline static function fromB<A, B>(b:B):Of2<A, B>
{
return Right(b);
}
@to inline static function toA():Null<A>
{
return switch (this)
{
case Left(a): a;
default: null;
}
}
@to inline static function toB():Null<B>
{
return switch (this)
{
case Right(b): b;
default: null;
}
}
}
abstract Of3<A, B, C>(Either<Either<A, B>, C>) from Either<Either<A, B>, C> to Either<Either<A, B>, C>
{
@from inline static function fromA<A, B, C>(a:A):Of3<A, B, C>
{
return Left(Left(a));
}
@from inline static function fromB<A, B, C>(b:B):Of3<A, B, C>
{
return Left(Right(b));
}
@from inline static function fromC<A, B, C>(c:C):Of3<A, B, C>
{
return Right(c);
}
@to inline static function toA():Null<A>
{
return switch (this)
{
case Left(Left(a)): a;
default: null;
}
}
@to inline static function toB():Null<B>
{
return switch (this)
{
case Left(Right(b)): b;
default: null;
}
}
@to inline static function toC():Null<C>
{
return switch (this)
{
case Right(c): c;
default: null;
}
}
}
abstract Of4<A, B, C, D>(Either<Either<A, B>, Either<C, D>>) from Either<Either<A, B>, Either<C, D>> to Either<Either<A, B>, Either<C, D>>
{
@from inline static function fromA<A, B, C, D>(a:A):Of4<A, B, C, D>
{
return Left(Left(a));
}
@from inline static function fromB<A, B, C, D>(b:B):Of4<A, B, C, D>
{
return Left(Right(b));
}
@from inline static function fromC<A, B, C, D>(c:C):Of4<A, B, C, D>
{
return Right(Left(c));
}
@from inline static function fromD<A, B, C, D>(d:D):Of4<A, B, C, D>
{
return Right(Right(d));
}
@to inline static function toA():Null<A>
{
return switch (this)
{
case Left(Left(a)): a;
default: null;
}
}
@to inline static function toB():Null<B>
{
return switch (this)
{
case Left(Right(b)): b;
default: null;
}
}
@to inline static function toC():Null<C>
{
return switch (this)
{
case Right(Left(c)): c;
default: null;
}
}
@to inline static function toD():Null<D>
{
return switch (this)
{
case Right(Right(d)): d;
default: null;
}
}
}
摘要/可打印.hx
package abstracts;
abstract Printable(Comparison.Of4<String, Int, Float, Bool>) {}
摘要/PrintableArray.hx
package abstracts;
abstract PrintableArray(Array<Printable.Printable>) {}