When I use ensuring { case something => some_boolean} the compiler tells me
Error:(132, 14) missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ?
} ensuring {
But when I use ensuring{ x => x match {case something => some_boolean} it compiles and works as expected.
Aha when I switch to 2.12 I get the same error. I can’t really explain it, but whatever the cause is seems to be fixed in 2.13.
The weirdest thing is that ensuring { case x => y } should be syntactic sugar for ensuring {_ match { case x => y }}. So it’s unexpected that one works and the other doesn’t.
Wouldn’t it be better to ensure that a UnitSqCoord cannot be created unless the conditions are met? I.e.,
case class UnitSqCoord(x: Int, y: Int) {
require(0 <= x && x <= 1, "x-coordinate must be between 0 and 1")
require(0 <= y && y <= 1, "y-coordinate must be between 0 and 1")
}
yes, I had done both. It’s only for debugging, as ensuring and requiring do not switch off automatically in deployed code. It is unfortunate that require and ensuring don’t disable themselves in non-debug mode, the programmer is forced to edit his code, risking introduction of new bugs.
Hmm. I’m not certain about ensure, but I’m fairly sure that require is supposed to go away in production-mode code, same as assert. But you do need to make sure that you’re telling the compiler to do so. Quoting the docs:
A set of assert functions are provided for use as a way to document and dynamically check invariants in code. Invocations of assert can be elided at compile time by providing the command line option -Xdisable-assertions , which raises -Xelide-below above elidable.ASSERTION , to the scalac command.