Seite 1 von 1

Unable to understand exam question Path dependent types

Verfasst: 12. Jul 2018 15:22
von AizazZaidee
Look at the following code example. The lines that start with [ ] contain definitions that might or might not type-check.
Please mark every line that type-checks with a ÿ and every line that does not type-check with an x.

Code: Alles auswählen

case class Board(length: Int, height: Int) {
case class Coordinate(x: Int, y: Int) {
require(0 <= x && x < length && 0 <= y && y < height)
}
val occupied = scala.collection.mutable.Set[this.Coordinate]()

val b1 = Board(20, 20)
val b2 = Board(30, 30)
val b3: Board = b1
val b4: b1.type = b1
val c1 = b1.Coordinate(15, 15)
val c2 = b2.Coordinate(25, 25)
val c3 = b3.Coordinate(10,10)
val c4 = b4.Coordinate(10,10)
[ ] b1.occupied += c1
[ ] b2.occupied += c2
[ ] b1.occupied += c2
[ ] b1.occupied += c3
[ ] b2.occupied += c3
[ ] b1.occupied += c4
[ ] b2.occupied += c4
}
What does means by type-check. I don't see any problem with the code? Anyone knows please help

Re: Unable to understand exam question Path dependent types

Verfasst: 12. Jul 2018 15:42
von nknauber
What does means by type-check. I don't see any problem with the code? Anyone knows please help
Why don't you just try to compile the code? If you try to do so, you will notice, that you will not be able to compile lines 3, 4, 5 and 7.

The question refers to whether or not the single lines of code will "type-check" as in "are they executable based on the provided types". For deeper understanding, refer to slides 28f. in slideset 2-PL-Design-Style.

Re: Unable to understand exam question Path dependent types

Verfasst: 12. Jul 2018 16:04
von AizazZaidee
Thanks, I tried the code but didn't compiled I thought Intellij will do so automatically but that is not the case. (but not in case of runtime exceptions, I get it thanks)

Re: Unable to understand exam question Path dependent types

Verfasst: 12. Jul 2018 16:11
von pmueller
Hi,

the Intellij parsers do not always work correctly(i.e. it marks code that is correct as incorrect and vice-versa) in the presence of path dependent types (and some other more advanced scala features), for these you should use sbt/scalac directly.

Best,
Patrick