1

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?

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113

1 Answers1

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.

Naam Famas
  • 124
  • 7
  • That's not entirely correct, `var h = new HashTable of string, Array of int (str_hash, str_equal)` works in Genie. So I had the correct syntax in the first place it's only that it doesn't work with simple arrays. – Jens Mühlenhoff Jul 12 '15 at 12:31