Skip to content

Tag Builder #276

@davesmith00000

Description

@davesmith00000

From Discord via @JD557, sounds like an interesting suggestion to me. I wonder if it can live in parallel with the current set up, or if it needs to replace it? Give it a try.


I'm not sure if this was discussed before (I think it might break bincompat, so it might be an issue), but are there any plans to improve the ergonomics of creating custom tags?

Here's what I mean: Right now tags are defined with for custom methods:

  def tag[M](name: String)(attributes: Attr[M]*)(children: Elem[M]*): Html[M] =
    Tag(name, attributes.toList, children.toList)
  @targetName("tag-list-repeated")
  def tag[M](name: String)(attributes: List[Attr[M]])(children: Elem[M]*): Html[M] =
    Tag(name, attributes, children.toList)
  @targetName("tag-repeated-list")
  def tag[M](name: String)(attributes: Attr[M]*)(children: List[Elem[M]]): Html[M] =
    Tag(name, attributes.toList, children)
  @targetName("tag-list-list")
  def tag[M](name: String)(attributes: List[Attr[M]])(children: List[Elem[M]]): Html[M] =
    Tag(name, attributes, children)

This is quite annoying to create new tags (and I think this is the reason for https://github.com/PurpleKingdomGames/tyrian/blob/3c3e6e7595e6d6e8e7444103c5c765de3978badc/project/TagGen.scala)

Wouldn't it be possible to instead have something like this?

case class TagBuilder(name: String):
  def apply[M](attributes: Attr[M]*)(children: Elem[M]*): Html[M] =
    Tag(name, attributes.toList, children.toList)
  @targetName("tag-list-repeated")
  def apply[M](attributes: List[Attr[M]])(children: Elem[M]*): Html[M] =
    Tag(name, attributes, children.toList)
  @targetName("tag-repeated-list")
  def apply[M](attributes: Attr[M]*)(children: List[Elem[M]]): Html[M] =
    Tag(name, attributes.toList, children)
  @targetName("tag-list-list")
  def apply[M](attributes: List[Attr[M]])(children: List[Elem[M]]): Html[M] =
    Tag(name, attributes, children)

def tag(name: String): TagBuilder = TagBuilder(name)

Then I think one could just write code like val table = tag("table") and things would just work.
(Maybe there are some exceptions to this that I'm not thinking about)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions