I have a project with a dozen classes that need to represent their private state as HTML. Each has a toHTML function. I'd like to pass that function a ScalaTags bundle so it can be rendered as either text (to print to a file) or as a DOM element for display directly in a browser.
Here's my attempt:
case class MyClass(...) extends ... {
// Render private state as HTML, either Text or DOM.
override def toHTML[Builder, Output <: FragT, FragT](
bundle: scalatags.generic.Bundle[Builder, Output, FragT]
): TypedTag[Builder, Output, FragT] = {
import bundle.all._
li("Here is a list element")
}
}
Even this simple example reports a compile-time error:
MyClass.scala:43:8: type mismatch;
[error] found : String("Here is a list element")
[error] required: scalatags.generic.Modifier[Builder]
[error] li("Here is a list element")
[error] ^
I've tried importing bundle.generic.implicits._ but it doesn't help and the compiler reports it as unused.
I'm aware that this is different from what the documentation shows in that I'm attempting to pass the bundle to the function rather than as a class parameter. I'd prefer to pass it to the function because toHTML is the only thing in a fairly complex class that has anything to do with HTML. I'd rather not clutter the entire class with stuff that only one function needs.
I've considered moving all the toHTML code from each of the dozen classes into a single class (using pattern matching to distinguish the cases). That would more closely match the documentation but means that I need to make a bunch of private state more visible. I'd rather not.
Any suggestions for how to get the above code to work? Is it a trivial error on my part or something that fundamentally can't work?