1

NOTE: my question is array, not [ Array or GenericArray ] this is my code:

init
    var h = new HashTable of string, Int? (str_hash, str_equal)
    h["a"] = Int ({1, 2, 3})
    h["b"] = Int ({5, 6, 7})    // HERE: WORKS FINE 
    // ERROR HERE: 
    // Array concatenation not supported for public array variables and parameters
    h["a"].data += 4

struct Int
    data: array of int
    construct (a: array of int)
        this.data = a

how to fix this?

Naam Famas
  • 124
  • 7

2 Answers2

1

Not really an answer, but an alternative way to express this:

init
    var h = new HashTable of string, Int? (str_hash, str_equal)
    h["a"] = Int ({1, 2, 3})
    h["b"] = Int ({5, 6, 7})
    h["a"].append ({4})

struct Int
    data: array of int
    construct (a: array of int)
        this.data = a
    def append (a: array of int)
        this.data = this.data + a

Now there is no mixing of "variables and parameters" going on anymore, which solves the compiler error your original code is triggering.

The problem is that this also results in a compiler error:

resize_array.gs:14.21-14.33: error: Incompatible operand
        this.data = this.data + a

Which can be simplified to this code:

init
    x: array of int = {1, 2, 3}
    y: array of int = {4, 5, 6}
    z: array of int = x + y

Which also produces the same compiler error.

resize_array.gs:21.23-21.27: error: Incompatible operand
    z: array of int = x + y

I have added a new question based on this:

How to concatenate two arrays?

As it turns out concating arrays (it works for string though!) is not a trivial task in Vala/Genie.

See the other question for solutions on how to do this.

I'd personally use Gee containers for this (if I don't have to frequently call some C functions that need a plain array).

The solution using Array of int:

init
    var h = new HashTable of string, Int? (str_hash, str_equal)
    h["a"] = Int ({1, 2, 3})
    h["b"] = Int ({5, 6, 7})
    h["a"].append ({4})

struct Int
    data: Array of int
    construct (a: array of int)
        data = new Array of int;
        append (a)
    def append (a: array of int)
        data.append_vals (a, a.length)
Community
  • 1
  • 1
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
0

A bug of Genie? maybe?

I try that in Vala, Works fine.

The output:

1, 2, 3, 4, 55, 666,

struct Int {
        int[] data;
        Int(int[] a){
                this.data = a;
        }
}

void main(){
        var h = new HashTable<string, Int?> (str_hash, str_equal);
        h["a"] = Int({1, 2, 3});
        h["b"] = Int({4, 5, 6});

        h["a"].data += 4;
        h["a"].data += 55;
        h["a"].data += 666;

        for (var i = 0; i < h["a"].data.length; i++) {
                stdout.printf("%d, ", h["a"].data[i]);
        }
}