# How can I extend a primitive type

**URL:** <https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 15, 2023, 2:04pm UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135 "2023-03-15T14:04:01Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [March 15, 2023, 4:59pm UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/21 "2023-03-15T16:59:50Z")

</div>

> [@stevengj](#):
>
> I think this is a perfect case for spelling them differently

Perhaps you’re right, I guess I like functions where the types are helping to document the function. This, again comes from Python where we type **hint** and rarely use multiple dispatch. I should re-think my Julia style.

> [@stevengj](#):
>
> (This is the problem with trying to argue about type design in the abstract or from unrealistic toy examples.)

This is something I’ve definitely learnt today.

edit:

Just to round off this discussion, what is the Julia equivalent of this python?

```python
>>> class Foo(float):
... def __init__ (self, v) -> None:
... self.value = v
...
>>> Foo(2)*2
4.0
>>> type(Foo(2))
<class ' __main__.Foo'>

```

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [March 15, 2023, 5:48pm UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/22 "2023-03-15T17:48:26Z")

</div>

Seeing that you’re trying to calculate future salaries, this is something that I implemented a while ago. My solution was just to use a couple of functions. I don’t have the exact code I used but it was something like this

```julia
isperc(x) = 0 < x < 1
new_salary(N, x) = isperc(x) ? N*(1+x) : N + x

```

Here, the raise amount could be a decimal (percent/100) percentage or the total amount (like $2000). This makes the assumption that all total dollar values would be more than $1 though.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [March 15, 2023, 6:17pm UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/23 "2023-03-15T18:17:30Z")

</div>

Interesting so you took a third approach. I think all this comes under the umbrella of coding style, perhaps I’m over-exuberant with multiple dispatch.

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [March 15, 2023, 6:22pm UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/24 "2023-03-15T18:22:03Z")

</div>

Yeah, I’m a “path of least resistance” coder and that solution seemed easiest at the time.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 15, 2023, 7:10pm UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/25 "2023-03-15T19:10:22Z")

</div>

> [@this\_josh](#):
>
> Just to round off this discussion, what is the Julia equivalent of this python?
> 
> ```julia
> >>> class Foo(float):
> 
> ```

In Julia you cannot make subtypes of concrete types — all concrete types are “final”. (This turns out to be crucial for performance!)

You can make a subtype of an abstract type like `Real` or `Number`, but then it is a lot more work — the generic `Real` type doesn’t have definitions for basic operations like `*` (how could it?). For a relatively simple example defining a new number type, see e.g. [Quaternions.jl](https://github.com/JuliaGeometry/Quaternions.jl).

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 15, 2023, 7:24pm UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/26 "2023-03-15T19:24:23Z")

</div>

If you are processing data, rather than relying on multiple dispatch later on, just make sure your data is conforming before doing any calculations. For the example you gave earlier, rather than having functions that do `x * (1 + change)` or `x * change`, I would make sure that all of my “change” data points (eg. in a DataFrame column) were in gross percent (90% = 1.9). Then I know `x * change` is going to be fine.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [March 16, 2023, 8:31am UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/27 "2023-03-16T08:31:58Z")

</div>

> [@stevengj](#):
>
> but then it is a lot more work

Okay, so it seems like this isn’t something worth doing for my purposes.

> [@stevengj](#):
>
> [Quaternions.jl](https://github.com/JuliaGeometry/Quaternions.jl)

Thank you, this clearly demonstrates the extra work required.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [March 16, 2023, 10:23am UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/28 "2023-03-16T10:23:29Z")

</div>

I don’t know if it’s a coincidence, but this just showed up: [GitHub - Mikumikunisiteageru/Logistics.jl](https://github.com/Mikumikunisiteageru/Logistics.jl)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 16, 2023, 10:25am UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/29 "2023-03-16T10:25:34Z")

</div>

Note that, by definition, one percent is one part per hundred (one per centum).

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [March 16, 2023, 10:29am UTC](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135/30 "2023-03-16T10:29:56Z")

</div>

Thank you for pointing this out, that looks like a handy package!

The timing is also a fun coincidence.

[Previous page](https://discourse.julialang.org/t/how-can-i-extend-a-primitive-type/96135.md?page=1)
