【发布时间】:2014-07-23 14:08:25
【问题描述】:
我需要一个基于泛型的类,它只适用于两种原始类型。这些类型彼此不相关(未实现,未相互扩展)。 有什么办法吗?
【问题讨论】:
-
haxe 的哪个版本?最新版本允许实验性 Java 支持。
-
如果这些类型之间没有关系,那么它们的交集是空的,不是吗?这样的限制有什么意义?你能分享一些代码来澄清一下吗?
我需要一个基于泛型的类,它只适用于两种原始类型。这些类型彼此不相关(未实现,未相互扩展)。 有什么办法吗?
【问题讨论】:
同意其他 cmets 和答案,但不知道您要达到什么目的,很难知道接受两种不相关类型的意义何在。
我能想到的几点:
1. 如果您从不以特定类型的方式使用对象(例如,您只将其序列化为 JSON,这适用于任何类型),那么您可以使用抽象包装动态:
举个例子,看看Andy Li's jQuery externs这个Either类:
abstract Either<T1, T2>(Dynamic) from T1 from T2 to T1 to T2 {}
然后你可以调用:
var arr:Array<Either<String,Int>>;
arr.push(0);
arr.push("hi");
var myInt = arr[0];
var myString:String = arr[1]; // you might need to use explicit type hints
这在幕后基本上是一个 Array,但它只允许您使用 String 或 Int。
2.如果您正在构建自己的类,并且只需要接受一种或另一种类型的参数,则可以只使用可选的函数参数:
function doSomething( ?myInt:Int, ?myString:String ) {
if ( myInt!=null ) trace('Int!');
else if ( myString!=null ) trace('String!');
}
3. 如果您想要1 之类的内容,但键入更严格,您可以使用更高级的摘要。冒着自我推销的风险,我详细写了a blog post,但我在这里粘贴了基本代码:
class Test {
static function main(){
stringOrInt( "hello" );
stringOrInt( 10 );
}
static function stringOrInt( either:AcceptEither<String,Int> ) {
switch either.type {
case Left(str): trace("string: "+str);
case Right(int): trace("int: "+int);
}
}
}
abstract AcceptEither<A,B> (Either<A,B>) {
public inline function new( e:Either<A,B> ) this = e;
public var value(get,never):Dynamic;
public var type(get,never):Either<A,B>;
inline function get_value() switch this { case Left(v) | Right(v): return v; }
@:to inline function get_type() return this;
@:from static function fromA( v:A ):AcceptEither<A,B> return new AcceptEither( Left(v) );
@:from static function fromB( v:B ):AcceptEither<A,B> return new AcceptEither( Right(v) );
}
enum Either<A,B> {
Left( v:A );
Right( v:B );
}
你可以像 var arr:Array<AcceptEither<String,Int>> = [] 这样使用任何泛型类。
【讨论】:
您可以使用 Map(haxe std lib) 中使用的方法。 Here is the current source.
【讨论】: