代码之家  ›  专栏  ›  技术社区  ›  Ceilingfish

scala方法语法的奇怪行为

  •  2
  • Ceilingfish  · 技术社区  · 15 年前

    我对scala的语法有点奇怪,我不太懂

    object Board {
       def getObjectAt(x:Int, y:Int):Placeable = return locations(x)(y)
    }
    

    很好用。但是

    object Board {
       def getObjectAt(x:Int, y:Int):Placeable {
          return locations(x)(y)
       }
    }
    

    返回错误

    Board.scala:8: error: illegal start of declaration
    return locations(x)(y)
    

    Placeable . 有什么方法可以解决这个问题,还是应该避免在这里指定返回类型?

    1 回复  |  直到 15 年前
        1
  •  10
  •   Dario    15 年前

    只是函数语法的问题。

    方程式 (使用 =

    object Board {
       def getObjectAt(x:Int, y:Int):Placeable = {
          return locations(x)(y)
       }
    }
    

    符号

    def func(...) { ...
    

    是返回类型的缩写 Unit ,即函数 没有 返回值。

    推荐文章