Genie中泛型方法的语法是什么?

时间:2015-06-28 07:23:40

标签: vala genie

我找到了一些Vala的代码,它运行正常。 但是当我把它翻译成精灵时,却失败了。 所以,我的问题是什么是Genie的等效代码

$scope

我的代码:精灵

for (var i = 0 ; i < $scope.Permissions.length; i++) {
    $scope['arr' + i] = $scope.Permissions[i];
}

console.log($scope.arr0, $scope.arr1);

错误消息:

int get_length<T> (T val) {
    if (typeof(T) == typeof(string) ) {
        return ((string)val).length;
    } else {
        GLib.error("Unable to handle type `%s'", typeof(T).name());
    }
}

public static void main() {
    var myString = "hello";
    stdout.printf("%i\n", get_length<string>(myString));
}

更新

代码有效。

def get_length of T (val: T): int
    if typeof(T) == typeof(string)
        return ((string)val).length
    else
        pass
init
    var s = "hello";
    stdout.printf("%i", get_length of string (s))

但如果我想要返回值

我试试

main.gs:2.16-2.17: error: syntax error, expected `(' but got `of' with previous identifier
def get_length of T (val: T): int
                ^^

和错误消息:

init
    printx of int (123)
    printx (456)
    printx ("HELLO")

def printx (i: T) of T
    case typeof(T)
        when 64 // typeof(string)
            stdout.printf ("%s\n", (string)i)
        when 24 // typeof(int)
            stdout.printf ("%i\n", (int)i)

并尝试

def doubleit (i: T): T of T

错误消息:

2015-06-29_generic_func.gs:13.27-13.27: error: The type name `T' could not be found
def doubleit (i: T): T of T
                          ^
2015-06-29_generic_func.gs:13.22-13.27: error: The type name `T' could not be found
def doubleit (i: T): T of T
                     ^^^^^^
2015-06-29_generic_func.gs:13.18-13.18: error: The type name `T' could not be found
def doubleit (i: T): T of T
                 ^
Compilation failed: 3 error(s), 0 warning(s)

此代码适用于Vala:

def doubleit (i: T) of T : T

0 个答案:

没有答案