Access function parameters from within ensuring

I have the following function for which the ensuring clause is failing.

  def locationToXy(loc: WorldLocation): Option[(Int, Int)] = {
    val WorldLocation(lat, lon) = loc
    if (lon < lonMin || lon > lonMax || lat < latMin || lat > latMax)
      None
    else {
      Some(unitSqToXy(projection.project(loc)))
    }
  } ensuring{ xy =>
    val m = xy match {
      case None => true
      case Some((x:Int,y:Int)) => (0 <= x) && (x < width) && (0 <= y) && (y < height)
    }
    if (!m){
      println(s"xy=$xy   width=$width  height=$height  em=$this")
    }
    m
  }

Is there a way for me to access the values which were passed to the function?

I can of course refactor the function to avoid ensuring altogether.

  def locationToXy(loc: WorldLocation): Option[(Int, Int)] = {
    val WorldLocation(lat, lon) = loc
    val xy = if (lon < lonMin || lon > lonMax || lat < latMin || lat > latMax)
      None
    else {
      Some(unitSqToXy(projection.project(loc)))
    }
    val m = xy match {
      case None => true
      case Some((x:Int,y:Int)) => (0 <= x) && (x < width) && (0 <= y) && (y < height)
    }
    assert(m,s"loc=$loc xy=$xy   width=$width  height=$height  em=$this")
    xy
  }

You should be able to access loc from the lambda passed to ensuring.

This simplified example works:

def foo(a: Int) = {
  a * 2
} ensuring { r =>
  r / 2 == a
}