score:0

apologies if i'm not answering your question... i don't know play, so i'm not clear on what request and response need... but when you ask for "a new method which replaces the need for both of these"... i think about something like this...

trait celestialbodyinfo
class suninfo extends celestialbodyinfo
class mooninfo extends celestialbodyinfo


def getresponse[t <: celestialbodyinfo](lat: double, lng: double) = action.async { request =>

  sw.makeservicecall(lat,lng).map { response =>

    response match {
      case jssuccess(r: t, path: jspath) => r match {
        case s: suninfo  => ok(views.html.temp1(s))
        case m: mooninfo => ok(views.html.temp2(m))
      }
      case e: jserror => ok(s"errors: ${jserror.tojson(e).tostring()}")
    }

  }
}

now you can call it like...

getresponse[suninfo](lat, lon)
getresponse[mooninfo](lat, lon)

or if you don't need to know when you call the function, and the request gets the right response automatically... something like this?

def getresponse(lat: double, lng: double) = action.async { request =>

  sw.makeservicecall(lat,lng).map { response =>

    response match {
      case jssuccess(r: celestialbodyinfo, path: jspath) => r match {
        case s: suninfo  => ok(views.html.temp1(s))
        case m: mooninfo => ok(views.html.temp2(m))
      }
      case e: jserror => ok(s"errors: ${jserror.tojson(e).tostring()}")
    }

  }
}

edit - responding to comment

could be something else going on here bc of play that i don't get... but that error message, in the general case, would mean that suninfo is not a subtype of celestialbodyinfo... which it needs to be based on the type bounds on type t.

example that creates the same error message

trait a {
  def x: int
}

class b(val x: int) extends a // class b is subtype of a 
class c(val x: int)           // class c is not a subtype of a 

// f requires an arg that is a subtype of a 
def f[t <: a](t: t) = t.x


f[b](new b(5))  // int = 5 
f[c](new c(5))  // error: type arguments [c] do not conform to method f's type parameter bounds [t <: a]

so when i see that error, my guess is that suninfo isn't a subtype of celestialbodyinfo, but it's hard to say what else could be going on.

score:0

try this:

def getsunormoonresponse(sunormoon: string,lat: double,lng: double) = 
  action.async{ request =>
      sunormoon match {
        case "sun" => getresponse[suninfo](new sunsw,lat,lng)(r => views.html.temp1(r))
        case "moon" => getresponse[mooninfo](new moonsw,lat,lng)(r => views.html.temp2(r))
      }
  }

protected def getresponse[t](sw: sw[t],lat: double,lng: double)(template: t => htmlformat.appendable): future[result] = {
  sw.makeservicecall(lat,lng).map{
    case jssuccess(r: t,path) => ok(template(r))
    case e: jserror => ok(s"errors: ${jserror.tojson(e).tostring()}")
  }
}

trait sw[+t] {
  def makeservicecall(lat: double,lng: double): future[jsresult[t]]
}

class sunsw extends sw[suninfo] {
  override def makeservicecall(lat: double, lng: double):future[jsresult[suninfo]] = ???
}
class moonsw extends sw[mooninfo] {
  override def makeservicecall(lat: double, lng: double): future[jsresult[mooninfo]] = ???
}

here i assumed that the sw that gets suninfo and the one that get mooninfo adhere to the same interface.


Related Query

More Query from same tag