0

这似乎在 Genie 的网站上已经过时了。可能不再支持 HashMap 或者它们的语法已更改。

如果尝试旧 BarryK网站上的示例:

uses
    Gee

init
    var d = new dict of string,string
    d["fruit"] = "apple"
    d["animal"] = "dog"
    d.set("plant","cactus")
    d.set("animal","hippopotomus") /*changes from 'dog'*/
    if d.contains("plant") == true do print "Key 'plant' is in dictionary"
    print "%s", d.get("animal")
    for o in d.keys do print o //old libgee use d.get_keys()
    for o in d.values do print o //old libgee use d.get_values()
    d.remove("animal")

一个dicts.gs:7.36-7.40: error: syntax error, embedded statement cannot be declaration以以下开头的行出现错误:

  • 如果 d.包含
  • 对于 d.keys 中的 o
  • 对于 d.values 中的 o

此外,使用 Genie 官方网站并没有太大的成功:

[indent=4]

uses
    Gee

init

    /* test dicts */
    var d = new dict of string,string

    /* add or change entries with following */
    d["Genie"] = "Great"
    d["Vala"] = "Rocks"


    /* access entires using d[key] */
    /* note that instead of "d.get_keys ()" it is "d.keys" in newer Versions of Gee */
    for s in d.get_keys ()
        print "%s => %s", s, d[s]

产生错误:dicts.gs:18.14-18.23: error: The name `get_keys' does not exist in the context of `Gee.HashMap<string,string>'for linefor s in d.get_keys ()

我错过了什么还是该网站已过时?

更新为了完整起见,我一直在使用 Manjaro linux,我的 libgee 包是 0.18 版本,编译时出现额外错误gee-0.8.vapi:664.4-664.13: warning: [Deprecated] is deprecated. Use [Version (deprecated = true, deprecated_since = "", replacement = "")]

4

1 回答 1

1

“嵌入式语句不能声明”消息是由于使用do关键字而不是开始一个新块引起的。如果您使用:

if d.contains("plant") == true
    print "Key 'plant' is in dictionary"

这将编译。或者,您可以print从语句更改为函数调用,这也将编译:

if d.contains("plant") == true do print( "Key 'plant' is in dictionary" )

一个工作示例,也为 Gee 版本 0.8 更新,将是:

[indent=4]
uses
    Gee

init
    var d = new dict of string,string
    d["fruit"] = "apple"
    d["animal"] = "dog"
    d.set("plant","cactus")
    d.set("animal","hippopotomus") /*changes from 'dog'*/
    if d.has_key("plant") == true do print( "Key 'plant' is in dictionary" )
    print "%s", d.get("animal")
    for o in d.keys do print( o )
    for o in d.values do print( o )
    d.unset("animal")

print我不知道语句和函数调用之间的区别print,但您似乎已经找到了。我猜解析器正在寻找do一个动作,一个动作应该是一个函数调用。

对于 Genie 教程中的示例,您错过了注释“请注意,在较新版本的 Gee 中,它不是“d.get_keys ()”,而是“d.keys”。Gee 0.8 实际上是较新的版本,所以你应该使用for s in d.keys. 我已经更新了教程以仅显示较新的版本,因为 Gee 0.8 已经存在很长时间了。工作示例是:

[indent=4]
uses
    Gee

init
    /* test dicts */
    var d = new dict of string,string

    /* add or change entries with following */
    d["Genie"] = "Great"
    d["Vala"] = "Rocks"

    /* access entires using d[key] */
    for var s in d.keys
        print "%s => %s", s, d[s]

关于该[Deprecated]属性被弃用的编译器警告是因为 Vala 0.32 将其替换为该[Version]属性,并且 Gee 绑定尚未更新。

于 2016-08-27T11:11:41.523 回答