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)
しかし、戻り値が必要な場合
私は試します
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) {