|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Defining an implicit value for a mixed traitHi all, I am trying to implement a function that uses implicit parameters according to a type parameter. But instead of specifying an implicit value for each possible type parameter, I would like to specify an implicit value only for specific traits that are mixed with the classes and use existential quantifier over the class itself. My implementation (which if course does not work) is given below.
I wanted to ask if it is possible to refine the implicit values so they will match not only the class but a mixed trait. ------------------------------------------------------------------------------------------------------------------------------------------------------------
// some class hierarchy class A class B extends A // the traits that should mark the implicit values trait T1 trait T2 extends T1
// some trait to be extended by the impilcit values but only according to a trait (T1 or T2) trait Printer[T <: T1] { def print(): Unit } object Test {
// a method which uses the implicit value. Here I try to make the implicit value fit any class which is mixed with a trait (T1 or T2) def pr[T <: T1](a: S with T forSome {type S <: A})(implicit p: Printer[T]) = p.print()
implicit object Printer1 extends Printer[T1] { def print() = Console.println("1") } implicit object Printer2 extends Printer[T2] {
def print() = Console.println("2") } } ------------------------------------------------------------------------------------------------------------------------------------------------------------
I try to call: pr (new A with T1) and I get: <console>:13: error: no implicit argument matching parameter type Printer[A with T1{ ... }] was found.
pr(new A with T1) If I will define the implicit objects to extend Printer[A] or Printer[B] then it will of course work but it would be nice to have implicit values corresponding to the traits.
Best regards, Tomer |
|
|
Re: Defining an implicit value for a mixed traitHi all,
It seems that my misunderstanding is of a much simpler issue regarding the currying only and not the implicit keyword. I have tried to supply the parameters explicitly and got an error as well. ------------------------------------------------------------------------------------------------------------------------------------------------------------ class A trait T1 trait Printer[S] {def print(): Unit} def pr[T <: T1](a: T)(p: Printer[T]) = p.print() pr(new A with T1)(new Printer[T1] {def print() = Console.println("1")}) ------------------------------------------------------------------------------------------------------------------------------------------------------------ The last command fails with the following error: <console>:9: error: type mismatch; found : java.lang.Object with Printer[T1]{ ... } required: Printer[A with T1{ ... }] pr(new A with T1)(new Printer[T1] {def print() = Console.println("1")}) ^ When I remove the currying everything works fine ------------------------------------------------------------------------------------------------------------------------------------------------------------ class A trait T1 trait Printer[S] {def print(): Unit} def pr[T <: T1](a: T, p: Printer[T]) = p.print() // implicit keyword removed pr(new A with T1, new Printer[T1] {def print() = Console.println("1")}) ------------------------------------------------------------------------------------------------------------------------------------------------------------ Returns the expected value 1. It seems to me that the compiler determination of the type of the partially-applied-function def pr[T <: T1](a: T) returns the wrong type. Can I somehow force the type of the returned function when currying? Thanks, Tomer 2009/11/2 Tomer Libal <shaolintl@...>
|
|
|
Re: Defining an implicit value for a mixed traitHi,
Further pinning the problem down to the problem of automatically inferring the type parameter of the function pr. If I explicitly supply this parameter the function works as well. pr[T1](new A with T1)(new Printer[T1] {def print() = Console.println("1")}) Is there a way I can supply enough information for this function to deduce the type parameter automatically in this example? Thanks, Tomer 2009/11/3 Tomer Libal <shaolintl@...> Hi all, |
| Free embeddable forum powered by Nabble | Forum Help |