Scala - 错误 "value map is not a member of"

Scala - error "value map is not a member of"

使用 Scala,我遇到了这个错误 "value map is not a member of chemins.Point" 点 <- segment.to

这是我的代码:

package chemins

case class Point(name: String, x: Long, y: Long)

case class Segment(from: Point, to: Point) {
  def distance: Double = {

    math.sqrt((from.x - to.x)*(from.x - to.x) + (from.y - to.y)*(from.y - to.y))
  }

}

case class Path(segments: Vector[Segment]) {

  def length: Double = {

    (for {

      segment <- segments

    } yield segment.distance).sum

  }

  def stops : Vector[Point] = {

    for {
      segment <- segments
      point <-  segment.to
    } yield point
  }


}

根据我之前的搜索,这似乎是因为 "to" 没有生成器,但我不知道它是否相关。

谢谢!

如果不看 chemins api,您很可能想要

for { segment <- segments } yield segment.to

for { 
  segment <- segments
  point = segment.to
} yield point

尝试将 segment.to 用作生成器毫无意义,因为它不是集合或 .map-pable 实体。

另请注意,此 for-comprehension 简化为

segments.map(_.to)