Genie 中泛型方法的语法是什么?
What is the syntax for generic methods in Genie?
我找到了 Vala 的一些代码,它工作正常。
但是当我把它翻译成精灵时,它失败了。
所以,我的问题是 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));
}
我的代码:精灵
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)
但是如果我想要return值
我试试
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
的等效代码是什么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)
但是如果我想要return值
我试试
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.
这似乎不可能