不是真正的答案,而是表达这一点的另一种方式:
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
现在不再混合“变量和参数”,这解决了您的原始代码触发的编译器错误。
问题是这也会导致编译器错误:
resize_array.gs:14.21-14.33: error: Incompatible operand
this.data = this.data + a
可以简化为以下代码:
init
x: array of int = {1, 2, 3}
y: array of int = {4, 5, 6}
z: array of int = x + y
这也会产生相同的编译器错误。
resize_array.gs:21.23-21.27: error: Incompatible operand
z: array of int = x + y
我基于此添加了一个新问题:
如何连接两个数组?
事实证明,连接数组(虽然它适用于字符串!)在 Vala/Genie 中并不是一项简单的任务。
有关如何执行此操作的解决方案,请参阅其他问题。
我个人会Gee
为此使用容器(如果我不必经常调用一些需要普通数组的 C 函数)。
使用的解决方案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)