gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:43:14 25 4
gpt4 key购买 nike

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

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));
}

我的代码: Sprite

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)

然后尝试

def doubleit (i: T) of T : T

错误信息:

2015-06-29_generic_func.gs:13.26-13.26: error: syntax error, expected end of line but got `:' with previous identifier
def doubleit (i: T) of T : T
^
Compilation failed: 1 error(s), 0 warning(s)

此代码适用于 Vala:

T doubleit<T> (T i) {

最佳答案

因为目前这似乎不可能,我有 reported it as a bug .

关于vala - Genie 中泛型方法的语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com