# Base.div vs Html.div - specialize or not?

**URL:** <https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776>\
**Category:** General Usage\
**Created:** [October 5, 2020, 9:35am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776 "2020-10-05T09:35:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [October 5, 2020, 9:35am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/1 "2020-10-05T09:35:36Z")

</div>

Dear Julians, I have a major dilemma. I’ve been going back and forth between how to handle this and I was wondering if there’s any best practice?

There is a `Html.div` function which creates a HTML `<div>...</div>` element. This is part of Genie’s HTML rendering API, together with many others, corresponding to the rest of HTML elements. These are exported to be used for writing expressive HTML UI code:

```julia
section() do 
  h1("Hello")
  p("Lots of text")
end

```

The issue is that `Html.div` conflicts with `Base.div` which is automatically exported by Julia. This makes `Html.div` an exception in the `Html` API in that it needs to be written as `Html.div(...)` or explicitly imported. This is ugly and verbose and can trip people new to Julia. So from a usability/principle-of-least-surprise perspective I’m all for specialising `Base.div`.

However, my left brain hemisphere screams that from a semantic standpoint `Base.div` and `Html.div` are totally different, and that it’s not specialisation as `Html.div` does not introduce a new division method.

In the end I’m partisan to doing what’s more natural for the users (least surprise is a big deal for me and a user of the `Html` API expects `div` to work like any other methods) but I was wondering what’s the community recommendation when dealing with such name conflicts, if any.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [October 5, 2020, 6:03pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/2 "2020-10-05T18:03:42Z")

</div>

I concur with the thought process.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 5, 2020, 6:22pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/3 "2020-10-05T18:22:53Z")

</div>

I would not extend `Base.div`, that is for division.

Regardless of the existence of `Base.div`, I think that using `Html.div`, `Html.p` etc are much more clear anyway, so personally I would just not export any of these.

---

<div class="post-metadata">

**Author:** ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)\
**Post date:** [October 5, 2020, 6:35pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/4 "2020-10-05T18:35:16Z")

</div>

Normally I prefer not exporting functions from packages as far as possible, but in this case we’re talking about code like

```julia
Html.div .... 
   Html.div ....
      Html.p ....
          Html.a ....
   Html.br
   Html.div
      Html.div
         Html.div

```

etc … which seems a bit toooo verbose

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [October 5, 2020, 6:36pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/5 "2020-10-05T18:36:05Z")

</div>

I’d pick a new name. [Hyperscript.jl](https://github.com/yurivish/Hyperscript.jl) uses `m()`. Similar JavaScript packages use `m()` or `h()`.

An option for the user is to do `const H = Html` to save a few characters, and then you can prefix everything.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 5, 2020, 6:38pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/6 "2020-10-05T18:38:40Z")

</div>

I guess, but you can always do

```julia
using Html: div, p, a

```

then.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [October 5, 2020, 7:14pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/7 "2020-10-05T19:14:24Z")

</div>

How about `html_div`?

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [October 5, 2020, 10:16pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/8 "2020-10-05T22:16:34Z")

</div>

Thanks for the input, much appreciated!

In the last few months I tried various approaches (and it’s true, this is an edge case in that the `Html` API is very large, which won’t be the case with most name conflicts with definitions from `Base`). My observations where:

1. when one has a 20-30-50 lines of HTML UI code, prefixing adds a lot of noise and extra typing. Also, it’s very clear from the context that we’re dealing with an HTML `div` so prefixing does not add value. This is more of an issue of a DSL really. @fredrikekre

2. in the beginning I didn’t export but I noticed that when rendering a reasonably complex HTML UI, explicitly listing all the used tags was a tedious process (imagine having to manually import some 20-30 tags). Also it was annoying as I would always forget to declare them, error, argghhh. @fredrikekre

3. @Mason I’ve seen this used in some other package’s APIs, but for consistency it means all the elements would have to be prefixed, so we’d end up with `html_p`, `html_h1`, etc. And in this case we’re better off using Julia’s “natural” namespaces, the modules, e.g. `Html.div`.

* * *

A “think outside the box” idea I just got would be to build a DSL out of constants. Then all the elements would be defined as `SPAN`, `DIV`, `H1`, `P` (uppercase tags would feel so 90s :)) ). But it seems very uncomfortable to type that many uppercase words… Although the idea might have some potential.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [October 5, 2020, 10:26pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/9 "2020-10-05T22:26:28Z")

</div>

How about an `@html` macro that just does find and replace to namespace things like `h1`, `p` and `div` so that they don’t need to be exported?

e.g. it would just take

```julia
@html section() do 
  h1("Hello")
  p("Lots of text")
  div("boo")
end

```

and turn it into

```julia
section() do 
  HTML.h1("Hello")
  HTML.p("Lots of text")
  HTML.div("boo")
end

```

That way, there’s a simply macro people can use but they can easily avoid it by just namespacing with `HTML` if they desire.

---

<div class="post-metadata">

**Author:** ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)\
**Post date:** [October 5, 2020, 10:48pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/11 "2020-10-05T22:48:08Z")

</div>

At one point, I started an experimental JSX.jl 🤓

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [October 5, 2020, 11:43pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/12 "2020-10-05T23:43:22Z")

</div>

If you don’t have to define `div` don’t do it and instead, let the users define their custom tags.

In [AcuteML](https://github.com/aminya/AcuteML.jl), I made HTLM/XML tag names independent of the Julia types. This allows high level of flexibility without unnecessary code bloat. I can pass any Julia type (String, Number, Array, etc) to a field without being worried about its name.

If your library is only used for HTML5, then it makes sense to define a type and call it `div`. However, for a general library like AcuteML that supports any arbitrary XML/HTML code with any tag names, this approach is not suitable.

```julia
using AcuteML
@aml mutable struct Body "~"
    group::Vector{String}, "div"
    heading, "h1"
    heading2, "h1-nonstandard"
    foo, "mytag"
end

```

> [@anon67531922](#):
>
> JSX.jl

AcuteML is like a Julia version of JSX. 😋 In the above, I defined `Body` as a component. Now, I can use it as a Julia type with any tag name!

```julia
@aml mutable struct Page doc"html"
    body::Body, "~"
end

```

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [October 6, 2020, 12:49am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/13 "2020-10-06T00:49:48Z")

</div>

I am pro-extending functions. Methods can have different “definitions” based on the context / arguments. That’s the point of multi-dispatch. `div(::AbstractString)` however would fall into type piracy though which is the only reason why I would not favor it.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [October 6, 2020, 6:58am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/14 "2020-10-06T06:58:50Z")

</div>

> [@Mason](#):
>
> How about an `@html` macro that just does find and replace to namespace things like `h1` , `p` and `div` so that they don’t need to be exported?

I would favor such solutions.

An other way of implementing the same kind of idea would be to have

```julia
@html section() do 
  h1("Hello $(div(5,4))")
  p("Lots of text")
  div("boo")
end

```

expand to something like

```julia
@html section() do 
  # Local function that dispatches to Base.div or Html.div
  div(args...) = Base.div(args...)
  div() = Html.div()
  div(x::AbstractString) = Html.div(x)

  h1("Hello $(div(5, 4))") # -> calls Base.div(5, 4)
  p("Lots of text")
  div("boo") # -> calls Html.div
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 6, 2020, 12:57pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/15 "2020-10-06T12:57:04Z")

</div>

Ideally two methods should belong to the same function when they do more or less the same thing. This makes writing generic code very easy .

Here this does not hold, so I would recommend that you have a separate `Html.div`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 6, 2020, 1:13pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/16 "2020-10-06T13:13:40Z")

</div>

> [@avik](#):
>
> etc … which seems a bit toooo verbose

If only there was a way to bring symbols in from a module without `using` :P.

```julia
julia> module Foo
           div(x) = print("<div>$x</div>")
       end

julia> using .Foo: div

julia> div("hiyo")
<div>hiyo</div>

```

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [October 7, 2020, 9:28am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/17 "2020-10-07T09:28:51Z")

</div>

I disagree with the viewpoint “`div` does division so it should never be used for another purpose”.  
This leads to people handicapping themselves, and then trying to find dubious justification for  
“creative” use of `div`. For me a method:

```julia
Base.div(x::foo)=...

```

is not pirating if the type `foo` is specific to your package. I do not see in which way this could  
introduce a conflict with `Base` or any other package. Thus a definition

```julia
Base.div(x::AbstractString)=...

```

is pirating (because if two packages do that there will be a conflict) but a definition

```julia
Base.div(x::HtmlString)=...

```

in a package devoted to html is not pirating.

---

<div class="post-metadata">

**Author:** ![jonas-schulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-schulze/32/9228_2.png) [@jonas-schulze](https://discourse.julialang.org/u/jonas-schulze)\
**Post date:** [October 7, 2020, 10:43am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/18 "2020-10-07T10:43:27Z")

</div>

Maybe a new language feature would be useful: a new import variant that allows for hiding several symbols. [Haskell](https://wiki.haskell.org/Import) has that and I think it is quite handy:

```haskell
import Base hiding (div)

```

In Julia it could maybe look something like this:

```julia
hiding Base: div

```

Though, this would need a [`baremodule`](https://docs.julialang.org/en/v1/base/base/#baremodule) or so, and it wouldn’t help users of older versions of Julia. I’ve opened an issue on GitHub.

[https://github.com/JuliaLang/julia/issues/37925](https://github.com/JuliaLang/julia/issues/37925)

---

<div class="post-metadata">

**Author:** ![jonas-schulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-schulze/32/9228_2.png) [@jonas-schulze](https://discourse.julialang.org/u/jonas-schulze)\
**Post date:** [October 7, 2020, 10:45am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/19 "2020-10-07T10:45:16Z")

</div>

So for this specific use case you mean this?

```julia
using HTML # to import div and others
using HTML: div # to resolve naming conflicts

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 7, 2020, 10:55am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/20 "2020-10-07T10:55:14Z")

</div>

> [@Jean\_Michel](#):
>
> I disagree with the viewpoint " `div` does division so it should never be used for another purpose".

You are of course free to use `div` to mean whatever you want in your own package but if you extend `Base.div` it should mean the same. Otherwise, you have no idea what generic code means because you don’t have concrete types available at that point. Maybe you forgot [Function name conflict: ADL / function merging?](https://discourse.julialang.org/t/function-name-conflict-adl-function-merging/10335) where this was explained in detail.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 7, 2020, 12:16pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/21 "2020-10-07T12:16:36Z")

</div>

> [@kristoffer.carlsson](#):
>
> if you extend `Base.div` it should mean the same. Otherwise, you have no idea what generic code means because

Base.div is always binary numeric arguments (or with third optional argument), while for HTML always (one?) string. Maybe I have “no idea”, but does it not save us if div for HTML is always a unary function? I’m thinking of the practical implications of specializing. I looked at your link to a much longer discussion than I have time for now… and from there a link to:

> [@Two modules with the same exported function name but different signature](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231):
>
> Hello, I have the following situation which I did not expected: defining a fucntion with the same name but different signature in two modules like this: ## Module RawTables function save(T::RawTable, name\_pref::String) ... end ## Module RawPlots function save(T::RawPlot, name\_pref::String) ... end and using them together results in WARNING: both RawPlots and RawTables export "save"; uses of it in module Main must be qualified I thought multiple dispatch is exactly good in being able to res…

I just know Julia for web programming, is also very important, so I hate to have this dilemma, and would like the most natural `div` (without needing macros or prefixing). Is this about mostly about the help text for div, that can’t be(?) extended. Can we allow for some impurity here?Would it make something slower?

SInce ÷ points to div (also for the help text), you could do very confusing…:

```julia
julia> ÷("text for div")
text for div

```

About the help text, should it be “x ÷ y” rather than current “÷(x, y)”?

[Next page](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776.md?page=2)
