Search code examples
scalascalatags

Scalatags, cannot return div() from other class


I'd like to split different html-parts into own classes and now I don't understand one thing regarding this code(-snippet):

val z = for(a <- 0 to 5) yield div(s"$a")
body(h1("Testing stuff"), z)

works just fine.

So does:

body(h1("Testing stuff"), test)
private def test = for(a <- 0 to 5) yield div(s"$a")

What does not work is this:

class Test[Builder, Output <: FragT, FragT](val bundle: scalatags.generic.Bundle[Builder, Output, FragT])
{
    import bundle.all._

    def render = for(a <- 0 to 5) yield div(s"$a")
}

val test = new Test(scalatags.Text)
body(h1("Testing stuff"), test.render)

This wont compile. It says (once the h1() is removed)

{quote} Type mismatch, expected: Modifier[Builder], actual: IndexedSeq[test.bundle.all.Div.Self] {quote}

And I simply do not understand why. Is there some implicit magic going on? Can one enlighten me and show me how it's done?

edit:

To further clarify the difference between mine and @Owen 's code:

In the other class (where I initialize a new Test) I did import bundle.all.all_ as well (derived from same base), but not import scalatags.Text.all._. So in the second package there seem to be some implicits that take care of the conversion. (Although I haven't found them yet.)

I also find it quite funny that this is not the same as import bundle.all._ when I am passing scalatags.Text as bundle after all.


Solution

  • Your code compiles fine for me. Here's the full code I compiled:

    object TagTest1 {
      class Test[Builder, Output <: FragT, FragT](val bundle: scalatags.generic.Bundle[Builder, Output, FragT]) {
        import bundle.all._
    
        def render = for(a <- 0 to 5) yield div(s"$a")
      }
    
      {
        import scalatags.Text.all._
    
        val test = new Test(scalatags.Text)
        body(h1("Testing stuff"), test.render)
      }
    }
    

    edit: I just encountered this error again today, googled it, and found this question. :)

    In case it helps anyone, I have since learned the following things:

    Debugging implicits

    Put the following in your build.sbt:

    scalacOptions ++= Seq(
      "-Xprint:typer"
    )
    

    (-Yprint:typer seems to have been removed).

    This particular implicit

    Based on -Xprint:typer, I determined the implicit needed to use a Seq of elements in another element is scalatags.Text.all.SeqFrag.