java - Scala pattern match types within JsValue -


so have these 2 similar functions in scala, both convert jsvalue java typed number , differ specific type:

import play.api.lib.json._ import java.lang.{double => javadouble, integer => javainteger}  def getdouble(jsdouble : jsvalue) : javadouble = {   val strdouble : string = jsdouble.asinstanceof[jsstring].value.replace(",", "")   val jdouble : javadouble = javadouble.valueof(strdouble)   jdouble }   def getinteger(jsinteger : jsvalue) : javainteger = {   val strinteger : string = jsinteger.asinstanceof[jsstring].value.replace(",", "")   val jinteger : javainteger = javainteger.valueof(strinteger)   jinteger } 

since they're similar, each other, hoping combine them 1 function , and use generic type differ 2 , pattern match type:

def getnumber[t](jsnumber : jsvalue) : t = {   val strnumber: string = jsnumber.asinstanceof[jsstring].value.replace(",", "")   t.valueof(strnumber) match {     case typeof[javainteger] => javainteger.valueof(strnumber)     case typeof[javadouble] => javadouble.valueof(strnumber)     // case _ => throw relevant exception    }  } 

however t.valueof doesn't work, i'm hoping there's way can pattern match type within jsvalue know type want return.

what closures ?

  def getnumber[t](jsnumber : jsvalue)(f : string => t) : t = {     val strnumber: string = jsnumber.asinstanceof[jsstring].value.replace(",", "")     f(strnumber)   }    def getdouble(jsdouble: jsvalue): javadouble = {     getnumber(jsdouble)(javadouble.valueof)   }    def getinteger(jsinteger: jsvalue): javainteger = {     getnumber(jsinteger)(javainteger.valueof)   } 

note @ point, syntax def getdouble = getnumber(_)(javadouble.valueof) valid, i'm not quite sure it's equivalent (or rather, i'm sure it's not, i'm not sure why :d )


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -