Nir
12/12/2020, 7:28 PMreturn when(Math.floorMod(value / 90 * rot.dir, 4)) {
0 -> vector
1 -> Point(-vector.j, vector.i)
2 -> Point(-vector.i, -vector.j)
3 -> Point(vector.j,-vector.i)
else -> throw Exception("impossible")
}
THe compiler here forces me to have the else. And I can't even have an assert False in the else, because that doesn't type check, I have to throw an exception which forces me to make up a name. Maybe something like !else at the end of the when or something like that. Or maybe some kind of special function with a contract that lets you do modulus where the compiler knows that only 0 to N-1 are possible outputsRob Elliot
12/12/2020, 7:35 PMakatkov
12/12/2020, 7:37 PMerror("impossible")
should also workFredrik Rødland
12/12/2020, 7:37 PMfun rotateRight(times: Int): Pos {
return when (times % 4) {
0 -> this
1 -> Pos(-y, x)
2 -> Pos(-x, -y)
3 -> Pos(y, -x)
else -> error("unable to rotate $this $times times")
}
}
Fredrik Rødland
12/12/2020, 7:39 PMerror
looks ok; not quite so overwhelming as the Throw Exception
, and more than once it has triggered after I changed my code. Even though it wasn’t possible when I wrote -it could be possible after changing it.Nir
12/12/2020, 7:40 PMNir
12/12/2020, 7:41 PMNir
12/12/2020, 7:41 PMNir
12/12/2020, 7:43 PMbrandonmcansh
12/13/2020, 2:55 AMbrandonmcansh
12/13/2020, 2:55 AMNir
12/13/2020, 3:13 AMNir
12/13/2020, 3:13 AMtateisu
12/13/2020, 3:46 PM