scala:动态作用域的内部类方法是什么?

发布于 2024-11-08 11:34:59 字数 482 浏览 0 评论 0原文

我正在尝试评估此处描述的所有 3 种动态范围界定方法(https:// /wiki.scala-lang.org/display/SYGN/Dynamic-scope),我了解除“内部类方法”之外的所有内容。描述如下:

使用嵌套类定义可以实现与动态作用域类似的效果。通过将整个状态消耗代码定义为状态对象的内部类,并在每次需要新的全局状态时实例化该对象,所有包含的代码都可以通过父引用直接访问状态变量。

为了避免在单个文件中定义整个程序,这种方法在大多数情况下都要求使用组件混合,以便将程序组合成单个类。

我不太明白这一点 - 有人可以给出一些示例代码来说明这一点吗?隐式参数的第二种方法对我来说很有意义,但文章还建议它可以与内部类方法结合使用,我也不太明白。谢谢!

i'm trying to evaluate all 3 methods of dynamic scoping described here (https://wiki.scala-lang.org/display/SYGN/Dynamic-scope) and i understand all but the "inner class method". it is described as follows:

It is possible to achieve a similar effect to dynamic scoping using nested class definitions. By defining the entire state-consuming code as inner classes of a state object, and instantiating that object each time a new global state is required, all the contained code gets direct access to the state variables via the parent reference.

To avoid defining the entire program in a single file, this approach for most purposes mandates the use of component mixins in order to compose the program into a single class.

I don't quite understand this - is it possible for someone to give some example code showing this? The second approach of implicit parameters makes sense to me, but the article also suggests that it can be combined with the inner class method and I don't quite see that either. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2024-11-15 11:34:59

像这样:

case class Board(rows: Int, columns: Int) {
    case class Pos(row: Int, column: Int) {
        require(0 <= row && row < rows && 0 <= column && column < columns)

        def neighbors = for {
            nRow <- Set(row - 1, row, row + 1)
            if 0 <= nRow && nRow < rows
            nColumn <- Set(column - 1, column, column + 1)
            if 0 <= nColumn && nColumn < columns
            if (nRow, nColumn) != (row, column)
        } yield Pos(nRow, nColumn)
    }
}

这里,Pos 指的是 Board 上的“上下文”:。例如:

scala> val board = Board(5, 5)
board: Board = Board(5,5)

scala> val pos = board.Pos(0, 0)
pos: board.Pos = Pos(0,0)

scala> println(pos.neighbors)
Set(Pos(0,1), Pos(1,0), Pos(1,1))

一个 Board 中的更改可以通过与该实例关联的 Pos 看到,但与其他实例关联则不然:

scala> val board2 = Board(2, 2)
board2: Board = Board(2,2)

scala> println(board.Pos(1,1).neighbors+"\n"+board2.Pos(1, 1).neighbors)
Set(Pos(1,0), Pos(1,2), Pos(2,0), Pos(2,1), Pos(0,0), Pos(2,2), Pos(0,1), Pos(0,2))
Set(Pos(0,0), Pos(0,1), Pos(1,0))

Like this:

case class Board(rows: Int, columns: Int) {
    case class Pos(row: Int, column: Int) {
        require(0 <= row && row < rows && 0 <= column && column < columns)

        def neighbors = for {
            nRow <- Set(row - 1, row, row + 1)
            if 0 <= nRow && nRow < rows
            nColumn <- Set(column - 1, column, column + 1)
            if 0 <= nColumn && nColumn < columns
            if (nRow, nColumn) != (row, column)
        } yield Pos(nRow, nColumn)
    }
}

Here, Pos refers to a "context" that is on Board: rows and columns. For example:

scala> val board = Board(5, 5)
board: Board = Board(5,5)

scala> val pos = board.Pos(0, 0)
pos: board.Pos = Pos(0,0)

scala> println(pos.neighbors)
Set(Pos(0,1), Pos(1,0), Pos(1,1))

Changes in one Board as seen by the Pos associated with that instance, but not with others:

scala> val board2 = Board(2, 2)
board2: Board = Board(2,2)

scala> println(board.Pos(1,1).neighbors+"\n"+board2.Pos(1, 1).neighbors)
Set(Pos(1,0), Pos(1,2), Pos(2,0), Pos(2,1), Pos(0,0), Pos(2,2), Pos(0,1), Pos(0,2))
Set(Pos(0,0), Pos(0,1), Pos(1,0))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文