# 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:** 10\
**Page:** 2

<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 7, 2020, 12:30pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/22 "2020-10-07T12:30:28Z")

</div>

> [@Palli](#):
>
> does it not save us if div for HTML is always a unary function?

Save us from what?

This is essentially a question of style — for generic programming, ideally all methods of a function do something similar. But there is nothing in the language enforcing this, and for your own types it is not even type piracy. So one can do

```julia
struct WackyType end
Base.div(::WackyType) = :wack

```

pretty much without any major adverse consequences; it is just poor style.

Minor consequences include confusing users using `methods`, documentation, and similar.

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [October 7, 2020, 12:35pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/23 "2020-10-07T12:35:50Z")

</div>

One additional option would be to turn all of the tags into string macros instead of functions:

```julia
div"hello world"(; class="shadowbox")

```

EDIT: nevermind. This doesn’t work for nested elements. Unless you instead had the contents of the string macro be irrelevant:

```julia
div""("hello world")

```

---

<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:45pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/24 "2020-10-07T12:45:03Z")

</div>

```julia
julia> methods(div) # I see my added test div buried in the middle...:
# 59 methods for generic function "div":

[29] div(s::AbstractString) in Main at none:1

[32] div(x::Bool, y::Bool) in Base at bool.jl:110

```

I wouldn’t worry about beginners looking at `methods`, I would at least worry more about `div` not being available, in the simplest form for HTML (I note, it’s rare to use div for division, and ÷ preferred by me, if not / or other operator, most users would).

This is more confusing:

```julia
julia> div(false, false)
ERROR: DivideError: integer division error
Stacktrace:
 [1] div(x::Bool, y::Bool)
   @ Base ./bool.jl:110
 [2] top-level scope
   @ none:1

julia> div(false, true)
false

```

If this is only about style and actually always working, I’m ok with impurity, want to make sure I’m not overlooking something (invalidations? making code loading slower?), e.g. could something like this be an issue:

```julia
[9] div(a::DataValues.DataValue{T1}, b::T2) where {T1, T2} in DataValues at /home/pharaldsson_sym/.julia/packages/DataValues/N7oeL/src/scalar/operations.jl:65

[13] div(::Number, ::Missing, r::RoundingMode) in Base at missing.jl:122

```

---

<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 7, 2020, 12:48pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/25 "2020-10-07T12:48:14Z")

</div>

> [@Palli](#):
>
> This is more confusing:
> 
> ```julia
> julia> div(false, false)
> ERROR: DivideError: integer division error
> 
> ```

This is just division by 0, what does this have to do with the topic we are discussing? 🤷‍♂️

---

<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, 1:27pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/26 "2020-10-07T13:27:05Z")

</div>

I did participate in this thread, where I was opposed to your opinion. I was a bit more in agreement with Jeff Bezanson. I actually wrote some software in reaction to this thread, see  
[Using\_merge.jl](https://discourse.julialang.org/t/using-merge-jl/45429)

---

<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 7, 2020, 6:34pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/27 "2020-10-07T18:34:11Z")

</div>

Aye, but that requires using a `HtmlString` or some other type that is defined in the package and the whole purpose is to decrease boilerplate so that would probably defeat the purpose.

For example, `stderr` in I/O context is one thing but in statistics is another. Would having it do two very different things sharing the name, why not? It is not context ambiguous. The issue is avoiding type piracy. Other than that, just make sure that the new methods have a clear docstring to avoid confusion.

---

<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, 8:04pm UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/28 "2020-10-07T20:04:58Z")

</div>

Yes, `stderr` is a good example of a term which has different meaning in different contexts, which is  
in my view the exact case were kristoffer’s assertion is needlessly limiting what Julia can do.

Another example where I “pirated” `Base` in my package is that in mathematics, conjugacy in a non-commutative group (that is, the conjugate of `g` by `h` is `inv(h)*g*h`) is denoted like exponentiation  
by a superscript `g^h`. It is thus natural to define

```julia
Base.:^(g::GroupElement,h::GroupElement)=inv(h)*g*h

```

An example of conjugacy in a non-commutative group is conjugacy of matrices, and I regret that  
there is no notation for it in Julia (as far as I know).

---

<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 8, 2020, 7:29am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/29 "2020-10-08T07:29:52Z")

</div>

> [@Jean\_Michel](#):
>
> example where I “pirated” `Base` in my package

Maybe I am missing some context, but if `GroupElement` is a type your package owns, the above code is not type piracy.

---

<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 9, 2020, 12:08am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/30 "2020-10-09T00:08:44Z")

</div>

Yes, in my view it is not piracy. But I guess it is in Kristoffer’s view which is why I used the term.  
And I hope that in your view it is not poor style either…

---

<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 9, 2020, 12:25am UTC](https://discourse.julialang.org/t/base-div-vs-html-div-specialize-or-not/47776/31 "2020-10-09T00:25:10Z")

</div>

Kristoffer never said it was piracy, he said it was a bad idea because it makes it hard to know what generic code does. The term I hear people use for this is ‘pun’.

Piracy is a bad idea because it breaks code, or at least makes it harder to not break code.

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