scala用于yield设置值

发布于 2024-08-26 07:07:52 字数 399 浏览 4 评论 0原文

我想创建 GridBagPanel.Constraints 列表。 我在 scala 编程书中读到,有一个很酷的 for-yield 构造,但我可能还没有理解它的正确工作方式,因为我的代码无法编译。如下:

        val d = for {
            i <- 0 until 4
            j <- 0 until 4
        } yield {
            c = new Constraints
            c.gridx = j
            c.gridy = i
        }

我想生成一个 List[Constraints] ,并为每个约束设置不同的 x,y 值,这样稍后当我添加组件时,它们将位于网格中。

I want to create a list of GridBagPanel.Constraints.
I read it in the scala programming book, that there is a cool for-yield construction, but I probably haven't understood the way it works correctly, because my code doesn't compile. Here it is:

        val d = for {
            i <- 0 until 4
            j <- 0 until 4
        } yield {
            c = new Constraints
            c.gridx = j
            c.gridy = i
        }

I want to generate a List[Constraints] and for every constraint set different x,y values so later, when I later add the components, they're going to be in a grid.

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

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

发布评论

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

评论(2

脸赞 2024-09-02 07:07:52

您只需在 yield 块末尾返回 c 即可获取 Constraints 集合。要让它返回 List,请使用 List 而不是 Range。就像这样:

val d = for {
            i <- List.range(0, 4)
            j <- List.range(0, 4)
        } yield {
            c = new Constraints
            c.gridx = j
            c.gridy = i
            c
        }

事实上,原始代码在 Scala 2.7 中不会执行您期望的操作,因为范围(如 Range 中)是不严格的。您可以在 Stack Overflow 或 Google 上查找它,但缺点是每次您在 d 上查找元素时,它都会创建一个新的 Constraint。 Scala 2.8 中此行为已更改。

You just need to return c at the end of the yield block to get a collection of Constraints. To get it to return a List, use a List instead of a Range. Like this:

val d = for {
            i <- List.range(0, 4)
            j <- List.range(0, 4)
        } yield {
            c = new Constraints
            c.gridx = j
            c.gridy = i
            c
        }

In fact, the original code would not do what you expected it to in Scala 2.7 because, there, ranges (as in Range) are non-strict. You may look it up on Stack Overflow or Google, but the short of it is that each time you looked up an element on d, it would create a new Constraint. This behavior has changed for Scala 2.8.

哭泣的笑容 2024-09-02 07:07:52

试试这个:

def conCreate = { 
    val c = new Constraints
    c.gridx = j
    c.gridy = i
    c
}

val d = for( i <- 0 until 4;
             j <- 0 until 4 ) yield conCreate(i,j)

我已经用对函数的调用替换了您的调用。我已将until 替换为Iterator.range(0,4),但我已将其返回到until。两者都是有效的代码,实际上含义相同。

Try this:

def conCreate = { 
    val c = new Constraints
    c.gridx = j
    c.gridy = i
    c
}

val d = for( i <- 0 until 4;
             j <- 0 until 4 ) yield conCreate(i,j)

I've replaced your call with a call to a function. I had replaced until with Iterator.range(0,4) but I've returned it to until. Both are valid code and actually mean the same thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文