Monday, 15 June 2015

scala - Signature of a function that takes an unkown number of inputs -



scala - Signature of a function that takes an unkown number of inputs -

try:

def big[t1, t2](func: t1 => t2) = func def small(t1: double, t2: double) = (t1, t2) big(small)

error:

type mismatch: expected (notinfered1) => notinferedt2, actual: (double, double) => (double, double)

same with:

def big[t1, t2](func: (t1*) => t2) = func

i believe parameter signature "big" should else accepts function takes unknown number of arguments.

here couple options:

1) using tupled - converts function function1 compiler happy (not user though :)):

scala> def big[t1, t2](func: t1 => t2) = func big: [t1, t2](func: t1 => t2)t1 => t2 scala> def small(t1: double, t2: double) = (t1, t2) small: (t1: double, t2: double)(double, double) scala> big(small) <console>:10: error: type mismatch; found : (double, double) => (double, double) required: ? => ? big(small) ^ scala> big(small _ tupled) warning: there 1 feature warning(s); re-run -feature details res1: ((double, double)) => (double, double) = <function1>

2) alter type of big take produces t:

scala> def big[t](func: => t) = func big: [t](func: => t)t scala> big(small _) res3: (double, double) => (double, double) = <function2> scala> def verysmall(t1: int) = t1 verysmall: (t1: int)int scala> big(verysmall _) res4: int => int = <function1>

note in first case it's function1 takes tuple , in sec case it's function2 takes 2 params.

looks vere looking ( => t) syntax.

scala

No comments:

Post a Comment