Answer the question
In order to leave comments, you need to log in
How do val parameters with the same name as parameterless methods work in Scala?
Let's say we have the following code in the scala worksheet:
class Point (val x: Int, val y: Int)
trait Rectangular {
def topLeft: Point
def bottomRight: Point
def left = topLeft.x
def right = bottomRight.x
def width = right - left
override def toString = topLeft.x + ", " + topLeft.y + " - " + bottomRight.x + ", " + bottomRight.y
}
class Rectangle(val topLeft: Point, val bottomRight: Point) extends Rectangular
val rect = new Rectangle(new Point(1,1), new Point(10, 10))
//rect: Rectangle = 1, 1 - 10, 10
rect.width
//res0: Int = 9
class Rectangle(override val topLeft: Point, override val bottomRight: Point) extends Rectangular
Answer the question
In order to leave comments, you need to log in
Design
class Rectangle(val topLeft: Point, val bottomRight: Point)
def topLeft: Point
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question