I'm pretty sure this is very simple to do in Scala, but I can't seem to figure out what hints the type system needs to make this work.
I want to have an abstract Printable class and then implicitly convert other classes to it. More specifically I want to implicitly convert Byte to Printable and Array[Byte] to Printable.
So I've done this:
abstract class Printable{
def print():String
}
class PrintableByte(b:Byte) extends Printable{
def print() = "" /*return something*/
}
implicit def printableByte(b:Byte) = new PrintableByte(b)
class PrintableArray(a:Array[Printable]) extends Printable{
def print() = {
for(i <- 0 until a.length) a(i).print() // no problems here
"" /*return something*/
}
}
implicit def printableArray(a:Array[Printable]) = new PrintableArray(a)
However:
val b:Byte = 0
b.print() //no problem here
val a= new Array[Byte](1024)
a.print() //error: value print() is not a member of Array[Byte]
I expected that the type system would be able to understand that Array[Byte] is implicitly an Array[Printable] and implicitly a Printable.
What am I missing?