I wrote a few functions that look like this:
def myWrite(os: OutputStream) = {} def myWrite(w: Writer) = {}
Now both are very similar, and I thought I'd try to write one parameterized version of the function.
I started with a type with two methods that are common in Java OutputStream and Writer:
type Writable[T] = { def close() : Unit def write(cbuf: Array[T], off: Int, len: Int): Unit }
One of the problems is that OutputStream writes Byte and writes Writer Char , so I parameterized the type with T
Then I write my function:
def myWrite[T, A[T] <: Writable[T]](out: A[T]) = {}
and try using it:
val w = new java.io.StringWriter() myWrite(w)
Result:
<console>:9: error: type mismatch; found : java.io.StringWriter required: ?A[ ?T ] Note that implicit conversions are not applicable because they are ambiguous: both method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A] and method any2Ensuring in object Predef of type [A](x: A)Ensuring[A] are possible conversion functions from java.io.StringWriter to ?A[ ?T ] myWrite(w)
I tried several other combinations of types and parameters until nothing came of it.
My question is, is there any way to achieve this at all, and if so, how.
(Note that to implement myWrite, it is necessary that the type T known inside, which parameterizes the write () method, because it needs to create a buffer, as in the new ArrayT array.)
UPDATE: the βrightβ solution does not work because the error is in the compiler: https://lampsvn.epfl.ch/trac/scala/ticket/2672