I am trying to write a conversions library for converting some scala types to an HTML representation. I would want, say, to execute List(1,2).toHtml and obtain <ul><li>1</li><li>2</li></ul>, as a String.
So far i've written a set of implicit conversions that work well on detecting the result type and applying the correct toHtml.
Let me show a sample:
object Conversions {
implicit def fromIterable[A](l : Iterable[A]) = new Object {
def toHtml = <ul> { l.map{ e => <li>{ e }</li> } } </ul> toString
}
}
import Conversions._
With this code, whenever i ask the compiler List(1,2).toHtml i get the correct conversion. As it would with any other Iterable val.
My problem and question here is how can i use this toHtml conversion recursively? Because if i typed List( List(1,2), List(3,4) ).toHtml i would want to get <ul> <li><ul><li>1</li><li>2</li></ul></li> <li><ul><li>3</li><li>4</li></ul></li> </ul>, the toHtml conversion applied recursively to every element of the input Iterable.
I tried to change the toHtml definition to def toHtml = <ul> { l.map{ e => <li>{ e.toHtml }</li> } } </ul> toString which didn't work because the compiler told me value toHtml is not a member of type parameter A which makes perfect sense.
I know that my problem probably resides in the new Object { ... } i am returning from the fromIterable[A] implicit definition, which should probably be returning a class with a trait or something.
I've tried reading a lot about implicits but i haven't figured it out yet out to be able to apply recursively this toHtml conversion without de-parametizing the fromIterable signature and defining several specific cases like fromIterable(l : List[List[Any]]) or something like that...
Can you guys please me give me some advices on how to achieve it and what am i doing wrong?
Thanks!