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.
Source: stackoverflow.com
Related Query
- How can I pass a class structure (in order to pattern match) & a template as method parameters
- how can i pattern match on runtime class in scala
- How can I pattern match on a range in Scala?
- How to pattern match a class with multiple argument lists?
- How is this case class match pattern working?
- In scala, how can I use pattern match to match a list with specified length?
- How can I use overloaded case class constructors and still use pattern matching?
- Can we assign / change traits to the scala class during runtime? How - any sample code? Like Strategy Pattern (of Gang of four Design Pattern)
- How to get the matched instance of case class inside match pattern
- How to make a parent class return a type which can match subtypes?
- Case Class to Json: Design Pattern on How to Avoid Repetitive Pattern Match without Using External Json Lib
- How can I pass a model to my main view template without code duplication in every controller?
- How can I reify a Symbol in order to pass it into runtime?
- Scala: How to pattern match a class type that extends a parent class?
- How do I get Scala's Option class so I can pass it to getDeclaredMethod()
- How can I find a play form type (for handling post requests in controller) in order to map a class containing BSONObjectID type?
- How can I pass a play template scala variable as input to a scala function?
- How to pattern match using regular expression in Scala?
- How to pattern match multiple values in Scala?
- How do I pattern match arrays in Scala?
- In Scala, how can I subclass a Java class with multiple constructors?
- Can we use match to check the type of a class
- How can a private class method be tested in Scala?
- How to pattern match large Scala case classes?
- How to pattern match on generic type in Scala?
- How can I pass extra parameters to UDFs in Spark SQL?
- How can I match classes in a Scala "match" statement?
- How can I pass JVM options to SBT to use when running the app or test cases?
- How can I create a read-only class member in Scala?
- How can I transform a Map to a case class in Scala?
More Query from same tag
- Get current date as string in databricks using scala
- libraries for external DSL evaluation in Scala
- scala/spark - group dataframe and select values from other column as dataframe
- scala - map.get returns None for null key but not for null value
- Scala's build.sbt not recognised by IntelliJ
- Akka: Future to Actor communication?
- Slick: how to return an error on update if zero row was updated
- scala - calling mysql stored procedure getting error : use right near '?)' at line 1
- Multiple Future calls in an Actor's receive method
- While using doodle from creativescala book I can't get my Image rotated by 180 degrees in recursive method
- How to get the latest value from a kafka Stream
- in what cases does scala properly convert String to java.lang.String
- Actorpublisher as source in handleMessagesWithSinkSource
- How to split an Array[Double] at point n
- Compare a key in a map with every string in a list and if they match then store it inside a new map with its corresponding value from the old map
- How to make implicits available to reflection
- Replace a method for usage only in a particular unit tests in Scala
- Does extending a class in scala with constructor params add vals (fields) to the class?
- how to deserialize DateTime in Lift
- Spark: convert rdd[row] to dataframe where one of the columns in the row is a list
- How to add a custom command in sbt?
- scala Play! object persistence is not a member of package javax
- How to narrow the type of an upper-bounded type parameter in a state machine encoding?
- Strange typecast error in sparkSQL.createDataFrame
- Default value on Left
- Empty output for Watermarked Aggregation Query in Append Mode
- Play-Scala-Slick hangs up
- Overriding method by return type in scala, oop solution?
- How to create a singleton object in Scala with runtime params
- How can I identify a remote actor?