|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
A question regarding type parameter inferenceHi all,
I have tried to use implicit values in a parameterized function and I found out that the type parameter of the function is not being inferred in the curried function (implicit values must be given curried). I wanted to ask if it is possible to force the type inference to infer only the trait type. An example is the following: --------------------------------------------------- class A trait T trait Printer[S <: T] {def print():Unit} def pr[S1 <: A, S2 <: T](a: S1 with S2)(implicit p: Printer[S2]) = p.print() --------------------------------------------------- I would like to be able to have the type of the argument decomposed into a class and a trait, so I can define the implicit value over the type of the trait only. Like in the following call: --------------------------------------------------- pr(new A with T)(new Printer[T] {def print() = Console.println("1")}) --------------------------------------------------- Here I get the error "required: Printer[A with T{ ... }]" but "found : java.lang.Object with Printer[T]{ ... }" Which seems to imply that the parameters of 'pr' get the most specific typing and is not decomposed. but it seems that such a decomposition of type is possible because if I dont use a curried version: --------------------------------------------------- def pr2[S1 <: A, S2 <: T](a: S1 with S2,p: Printer[S2]) = p.print() // not curried pr2(new A with T,new Printer[T] {def print() = Console.println("1")}) // no problem. --------------------------------------------------- the call succeeds . It seems to me that in the curried version we are working in two phases and first define the type parameter of the partially-applied function to be 'A with T{ ... }' although I specify its type to be <: T. Of course, if I supply the trait type as a parameter to the curried function it also infers the type correctly. I apologize in advance if my formulation of the problem is not very clear. I try to ask several questions about the type inference of a trait as type parameter, about the type inference of type parameters in curried functions and about the possibility to decompose types using sentences like [T,S] T with S. The main idea is that I want to use implicit parameters which are identified according to traits and not classes and in the curried functions it seems to me the type is inferred in a too specific way (A with T) and not as T. I think it is also more intuitive to have the type inference behave the same in the curried and un-curried versions. Thanks, Tomer |
|
|
Re: [scala] A question regarding type parameter inferenceBy design, type inference only considers the first list of arguments (similarly for overloading, which is decided purely on the type of the arguments of the first argument list).
hth adriaan On Thu, Nov 5, 2009 at 5:17 PM, TomerL <shaolintl@...> wrote:
|
|
|
Re: [scala] A question regarding type parameter inferenceThanks, this helped. I understand now this problem can also be solved by using type parameters in the argument given (and not to "Decompose" the type).
Thanks again, Tomer
2009/11/5 Adriaan Moors <adriaan.moors@...> By design, type inference only considers the first list of arguments (similarly for overloading, which is decided purely on the type of the arguments of the first argument list). |
| Free embeddable forum powered by Nabble | Forum Help |