【问题标题】:"Embedded statement cannot be declaration" error in GenieGenie 中的“嵌入式语句不能声明”错误
【发布时间】:2023-04-07 16:30:01
【问题描述】:

这在 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

  • 如果 d.contains
  • d.keys 中的 o
  • d.values 中的 o

另外,使用官方精灵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 line 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 = "")]

【问题讨论】:

    标签: genie


    【解决方案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 绑定。

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多