I think this doesn’t work because ensuring is expecting a type of type Any so you have to do a case match on the variables but then you end up naming the constituents of the tuple which I don’t need to?
I get confused because sometimes underscores refer to consecutive parameters so they refer to different items - is this what is going wrong here?
ensuring expects a function A => Boolean, where A is the type of the previous expression. So, in your case it expects (Int, Int) => Boolean, which is what you want.
The reason your code does not work is that you are trying to have two different underscores refer to the same argument (also, underscore notation only works with “simple” expressions, and I’m not sure yours is simple enough).
Thanks for both responses. It now works using curoli’s method.
Regarding the case statement, I get the following:
scala> def ab(n: Int): (Int, Int) = { (n, n+10) } ensuring { case (x,y) => x != 1 && y != 11 }
<console>:11: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ?
def ab(n: Int): (Int, Int) = { (n, n+10) } ensuring { case (x,y) => x != 1 && y != 11 }
^
Sorry about that. There was a PR to help inference.
snips $ scalam
[info] started at Thu Aug 30 14:47:53 PDT 2018
Welcome to Scala 2.13.0-M4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_144).
Type in expressions for evaluation. Or try :help.
scala 2.13.0-M4> def ab(n: Int): (Int, Int) = { (n, n+10) } ensuring { case (x,y) => x != 1 && y != 11 }
ab: (n: Int)(Int, Int)
scala 2.13.0-M4> :quit
snips $ scala -Dscala.repl.info
Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_144).
Type in expressions for evaluation. Or try :help.
[info] started at Thu Aug 30 14:48:09 PDT 2018
scala 2.12.6> def ab(n: Int): (Int, Int) = { (n, n+10) } ensuring { case (x,y) => x != 1 && y != 11 }
<console>:11: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ?
def ab(n: Int): (Int, Int) = { (n, n+10) } ensuring { case (x,y) => x != 1 && y != 11 }
^
scala 2.12.6> :quit