functional programming - scala pass type parameter function as a parameter of another function -


suppose,i have function, take 2 values , function parameters.

def ls[s](a: s, b: s)(implicit evl: s => ordered[s]): boolean = < b def myfunction[t](a: t, b: t, f:(t,t)=>boolean) = {    if (f(a, b)) {     println("is ok")   }   else {     println("not ok")   } }  myfunction(1, 2, ls) 

the ide don't give error message,but when try compile , run,the compliter give message:

    error:(14, 19) no implicit view available s => ordered[s]. myfunction(1, 2, ls);}                  ^ 

so,is there way pass type parameter function parameter of function ?

firstly works:

myfunction[int](1, 2, ls) myfunction(1, 2, ls[int]) 

from understanding scala compiler tries resolve type t of myfunction.

it finds first , second argument , happy assign int (or supertype) best match.

but third argument says can parameter! scala compiler must comply , decides t any. causes s of type any , there no implicit view any => ordered[any]

you must not think of myfunction defining type ls rather ls defining type t can be.

which why work know type of f want:

def myfunction[t](a: t, b: t)(f:(t,t)=>boolean) = { ... } myfunction(1, 2)(ls) 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -