Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loose type specification on Blueprint::HTML::Utils#plain(content : String) from String to any #77

Closed
stephannv opened this issue Oct 11, 2024 Discussed in #76 · 0 comments · Fixed by #78
Closed

loose type specification on Blueprint::HTML::Utils#plain(content : String) from String to any #77

stephannv opened this issue Oct 11, 2024 Discussed in #76 · 0 comments · Fixed by #78
Assignees

Comments

@stephannv
Copy link
Owner

Discussed in #76

Originally posted by robacarp October 11, 2024
I'm normally in favor of a strictly typed interface, especially at the boundary layer of a shard. But I wonder if the expressiveness here might actually benefit from loosening it up a bit where content is passed into a tag renderer.

For example this method declaration has a strict argument type signature of String where it's not actually needed:

  private def plain(content : String) : Nil
    append_to_buffer(content)
  end

That results in needing to explicitly .to_s a number, where that's unlikely to be actually necessary:

   # explicit cast of data to string, because the compiler will complain without it
   plain query.count.to_s

What do you think about adding an overload method which calls .to_s on the content?

  private def plain(content) : Nil
    append_to_buffer(content.to_s)
  end

The .to_s is naïve, but it'll result in not needing to manually call .to_s on every non-string which has a reasonable .to_s implementation. Consider this:

    h2 do
      plain "Showing "
      plain query.current_range.begin.to_s
      plain " to "
      plain query.current_range.end.to_s
      plain " of "
      plain query.total_hits.to_s
      plain " results"
    end

This type of desired result is the worst case for this style of html templating engine like blueprint or luckytemplate -- it requires a lot of overhead to compile a simple string, and in this case each data point being injected requires an explicit .to_s as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant