How can I match a function signature without getting type erasure compiler warnings in Scala -


Can anyone rewrite this code to do the same thing, but without any compiler warning: -

  Object TestTypeErasure {def main (args: Array [String]) {def myFunction (myObject: Any): Boolean = {true} val myVariable: (Any => Boolean) = myFunction MyVariable match {case function: (any => Boolean) = & gt; Println ("match")}}}  

thousand thankyous

Keith

update !!!!. Sorry, making a real hash of this. My first question is on SO

Just to tell the master that I have not tried anything in any of its forms: - (It can not be compiled)

  Object TestTypeErasure {def doTest (parameter: any) = {parameter match {case function: Function1 [_, _] = & gt; I get the error: -  
  Test type ("whatever")} def main (arg: array [string]) {}}  

Erasrasecoll: 6: Error: Type mismatch; Found: java.lang.String ("whatever") is required: _case function: function 1 [_, _] = & gt; Function ("whatever") ^ An error occurred

thanks again

Keith

You can capture type information with the manifest. (T, R is irreversible here to keep things simple.)

  Import Slalah. Reflect._ def matchFunction [T, R] (f: function 1 [t, r], t: t) (vested MT: manifest [t], MR: manifest [r] = = (core result = if ((mt .erasure, mr.erasure) == (classoff [some], class [boolean]) "any, boolean" + f (t) else if ((mt.erasure, mr.erasure) == (classOf [Int ], "Int, int" + f (t) other "unknown" + f (t) println (result)} skeleton; match function (x: int) => x + 1, 1) Int, int2 scale> match function (x: any) => true, 1: any) Any, boolean true scale>  
 < / P>      Code> import scala.reflect._ def matchFunction [T: Man FAFF, R: MANIFEST] (F: Function 1 [T, R], T: T) = {val mt = implicitly [manifest] [t]] val mr = implicitly [manifest] [result] val = if ((mt .erasure, mr.erasure) == (classOf [Any], classOf [boolean]) "Anyone, boolean" + f (t) else if ((mt.erasure, MR.Royer) == (classOf [Int ], "Int, int" + f (t) other "unknown" + f (t) println (result)}  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -