What is the syntax for nested generic types in Genie?

狂风中的少年 提交于 2020-01-11 11:34:12

问题


I want to declare a HasTable with string as it's key and array of int as it's value:

[indent=4]

init
    var h = new HashTable of string, array of int (str_hash, str_equal)
    h["a"] = {1, 2, 3}
    h["b"] = {5, 6, 7}

Error message:

nested_generic_types.gs:4.27-4.28: error: syntax error, expected line end or semicolon but got `of'
    var h = new HashTable of string, array of int (str_hash, str_equal)

So the double of seems to confuse valac here.

What is the proper syntax?


回答1:


The error message is diffrent from vala.

Genie's error message looks like a compiler's parse problem. vala's error message is more clear.

my test in vala:

void main () {
    var h = new HashTable<string, int[]> (str_hash, str_equal);
}

error message:

error: `int[]' is not a supported generic type argument, 
use `?' to box value types

looks like just not support "array", and others all works. 'array' can't be an element in any container(HashTable, Array, GenericArray, array..)

some test: all works!

[indent=4]

init
    var h = new HashTable of string, HashTable of string, int (str_hash, str_equal)
    h["a"] = new HashTable of string, int (str_hash, str_equal)
    h["a"]["b"] = 123
    stdout.printf ("%d\n", h["a"]["b"])

    var a = new HashTable of string, Array of int (str_hash, str_equal)
    a["a"] = new Array of int
    // a["a"].append_val (456)
    // error: lvalue expected
    var x = 456
    a["a"].append_val (x)
    stdout.printf ("%d\n", a["a"].index(0))


    var b = new HashTable of string, GenericArray of int (str_hash, str_equal)
    b["a"] = new GenericArray of int
    b["a"].add (567)
    stdout.printf ("%d\n", b["a"].get (0))

    var d = new array of Array of int = {new Array of int(), new Array of int}
    // ERROR IF {new Array of int, new Array of int}
    var y = 321
    d[0].append_val (y)

    stdout.printf ("%d\n", d[0].index(0))

an explanation from: http://blog.gmane.org/gmane.comp.programming.vala/month=20140701

No correct syntex, It just not support that.



来源:https://stackoverflow.com/questions/31358261/what-is-the-syntax-for-nested-generic-types-in-genie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!