"Embedded statement cannot be declaration" 精灵错误

"Embedded statement cannot be declaration" error in Genie

Genie 网站上的内容似乎已过时。可能不再支持 HashMap 或它们的语法已更改。

如果尝试旧版 BarryK 中的示例 website:

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

此外,使用官方精灵website也没有太大成功:

[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 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 = "")]

"embedded statement cannot be declaration" 消息是由于使用 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 教程中的示例,您错过了评论 'note that instead of "d.get_keys ()" it is "d.keys" in newer Versions of Gee'。 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 绑定尚未更新。