Unable to understand exam question Path dependent types

Moderatoren: pmueller, SE - Design and Construction

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Unable to understand exam question Path dependent types

Beitrag von AizazZaidee » 12. Jul 2018 15:22

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

nknauber
Erstie
Erstie
Beiträge: 11
Registriert: 25. Apr 2018 16:48

Re: Unable to understand exam question Path dependent types

Beitrag von nknauber » 12. Jul 2018 15:42

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.

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: Unable to understand exam question Path dependent types

Beitrag von AizazZaidee » 12. Jul 2018 16:04

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)

pmueller
Moderator
Moderator
Beiträge: 87
Registriert: 25. Jan 2018 11:14

Re: Unable to understand exam question Path dependent types

Beitrag von pmueller » 12. Jul 2018 16:11

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

Antworten

Zurück zu „Software Engineering - Design and Construction“