Cast类型参数必须为Dynamic

时间:2016-11-08 17:05:42

标签: casting haxe openfl

我刚开始使用haxe(moving from AS3)

我正在尝试转换为Array,但它不起作用,即当我尝试编译时出现Cast type parameters must be Dynamic错误。以下是我使用的代码:

    var result: Array<String> = cast(["sometext"], Array<String>);        

    var arr: Array<Int> = new Array<Int>();
    arr.push(1);
    var vect : Array<Int> = cast(arr, Array<Int>);

    var arr1: Array<Int> = [1, 2 ,3];
    var vect1 : Array<Int> = cast(arr1, Array<Int>);

    var arr2 = [1, 2 ,3];
    var vect2 : Array<Int> = cast(arr2, Array<Int>);

这四个演员阵容中的每一个都不会编译并给出相同的错误“演员类型参数必须是动态的”。如果我将类型参数更改为动态,它也将无效。它失败并出现不同的错误。

有人可以解释为什么这种投射方式不起作用以及如何投射到数组?

其他演员:

    var i: Int = 1;
    var j: Int = cast(i, Int);

    var str: String = "str";
    var str1: String = cast(str, String);

    var instance: CastToArrayTest = new CastToArrayTest();
    var instance1: CastToArrayTest = cast(instance, CastToArrayTest);

工作得很好。

1 个答案:

答案 0 :(得分:0)

我认为用一些示例代码解释可能更容易:

class Test {
    static function main() {
        var a = ["foo", "bar"];
        $type(a);  // Warning : Array<String>

        // no need to cast or type simple assignments
        var b = a;
        $type(b);  // Warning : Array<String>

        // but lets define `x` as `Dynamic` (or something that we don't know what it is)
        var x:Dynamic = ["foo", "bar"];
        $type(x); // Warning: Dynamic

        // we can simply assign it to an `Array<Dynamic>` variable (but we don't get any runtime checks!)
        var c:Array<Dynamic> = x;
        $type(c);  // Warning: Array<Dynamic>

        // we can also simply assign it to an `Array<String>` variable (but we don't runtime checks either!!)
        var d:Array<String> = x;
        $type(d);  // Warning: Array<String>

        // (if we want runtime checks, we need to code them with the `Type` apis)

        // now, let's say we want to do
        //     var e:Array<String> = c;
        // but that fails with `Type parameters are invariant`...
        // we can instead use an unsafe cast
        var e:Array<String> = cast c;
        $type(e);  // Warning: Array<String>

        // type parameters are a compile time only feature
        trace(showType(a), showType([1,2]));
        trace(Std.is(a, Array), Std.is([1,2], Array));
        // safe casts only check the current object (not its children), so we need to use them with `Dynamic` parameters
        var f = cast(x, Array<Dynamic>);
        $type(f);  // Warning: Array<Dynamic>
        // however, due to variance, we can't assign a the safe cast result of type `Array<Dynamic` to a `Array<String>` variable without an unsafe cast...
        // which makes sense: our safe cast only checks that the object is an array, but the array doesn't know its own type nor has the safe cast checked the types of the children
    }

    static function showType(v:Dynamic)
    {
        switch Type.typeof(v) {
        case TClass(cl):
            return Type.getClassName(cl);
        case other:  // TODO handle other types
            return Std.string(other);
        }
    }
}

您可以在Try Haxe #CCaD5播放。

换句话说,在这里&#34;演员&#34;示例通常会起作用并且看起来像:

var result: Array<String> = cast ["sometext"];

var arr: Array<Int> = new Array<Int>();
arr.push(1);
var vect : Array<Int> = arr;

var arr1: Array<Int> = [1, 2 ,3];
var vect1 = arr1;

var arr2 = [1, 2 ,3];
var vect2 = arr2;

var i: Int = 1;
var j: Int = i;
var k = j;

var str: String = "str";
var str1: String = str;
var str2 = str1;

var instance: CastToArrayTest = new CastToArrayTest();
var instance1: CastToArrayTest = instance;
var instance2 = instance1;

您可以在Try Haxe #97ADc上看到这个(有一些额外的信息)。