# New concrete number type

**URL:** <https://discourse.julialang.org/t/new-concrete-number-type/55210>\
**Category:** General Usage\
**Tags:** type\
**Created:** [February 13, 2021, 3:08pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210 "2021-02-13T15:08:05Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [February 13, 2021, 5:08pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/21 "2021-02-13T17:08:16Z")

</div>

Thanks @johnmyleswhite . It’s clear from the comments above that this is more hassle than it’s worth for my case. All I want is to enforce the bounds on assignment. I understand @carstenbauer 's point, but that’s not a constraint for what I have in mind.

Thanks for the reference to _refinement type_: it’s helpful to know the right terminology when searching.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [February 13, 2021, 5:11pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/22 "2021-02-13T17:11:13Z")

</div>

@carstenbauer You’re right: it’s the wrong reference. You’re making a good point, just not one that is relevant for what I have in mind. I should have been clearer.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 13, 2021, 5:17pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/23 "2021-02-13T17:17:58Z")

</div>

> [@smldis](#):
>
> For errors on array element assignment you can assert it when you define `setindex!` .

You don’t need that. The error will be thrown when `setindex!` tries to convert to the correct element type.

---

<div class="post-metadata">

**Author:** ![smldis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smldis/32/1735_2.png) [@smldis](https://discourse.julialang.org/u/smldis)\
**Post date:** [February 13, 2021, 5:21pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/24 "2021-02-13T17:21:47Z")

</div>

Assignment in julia is not like C++ where you can overload it.  
Let’s say you run `ptot = p1 + p2`. If you want an error when ptot is greater than 1 you need to do the check by coding it in the `+` method that actually get used there.  
Also mathematically, a sum of probabilities is not always a probability and can be greater than 1 like @carstenbauer noted.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 13, 2021, 5:25pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/25 "2021-02-13T17:25:02Z")

</div>

> [@smldis](#):
>
> If you want an error when ptot is greater than 1 you need to do the check by coding it in the `+` method that actually get used there.

I think the right check should happen on construction/conversion, not necessarily by each method, like `+`.

I agree that checking on _assignment_ doesn’t make sense.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [February 13, 2021, 5:26pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/26 "2021-02-13T17:26:05Z")

</div>

Yeah, construction/conversion.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [February 13, 2021, 5:27pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/27 "2021-02-13T17:27:40Z")

</div>

If this is all you want, you should copy the example of Float16 instead: [julia/boot.jl at 66c9f6a9b58130d35fa9cf10a7e16daf5847e575 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/66c9f6a9b58130d35fa9cf10a7e16daf5847e575/base/boot.jl#L213)

But it doesn’t seem like it’s true that you want _only_ this: it sounds like you want “all existing methods for Float64, except for the constructor”, which is kind of the mirror image of creating a new primitive type.

---

<div class="post-metadata">

**Author:** ![smldis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smldis/32/1735_2.png) [@smldis](https://discourse.julialang.org/u/smldis)\
**Post date:** [February 13, 2021, 5:33pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/28 "2021-02-13T17:33:53Z")

</div>

Oh yes, it doesn’t need to be on the method because you will call the constructor that does the check itself.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [February 13, 2021, 5:46pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/29 "2021-02-13T17:46:48Z")

</div>

Thanks @johnmyleswhite . I’ll have a look.

But yes, I want all methods to behave exactly as though they are floats except the constructor which should do bounds checking.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [February 13, 2021, 5:50pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/30 "2021-02-13T17:50:42Z")

</div>

Perhaps it’s easier to make a function that looks like a constructor and makes “Probability” objects that are really Float64 objects?

```julia
julia> function Probability(x::Float64)
       if x < 0.0 || x > 1.0
       error("Invalid probability")
       end
       x
       end
Probability (generic function with 1 method)

julia> Probability(0.5)
0.5

julia> Probability(1.5)
ERROR: Invalid probability
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] Probability(x::Float64)
   @ Main ./REPL[1]:3
 [3] top-level scope
   @ REPL[3]:1

```

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [February 13, 2021, 5:52pm UTC](https://discourse.julialang.org/t/new-concrete-number-type/55210/31 "2021-02-13T17:52:15Z")

</div>

Thanks @johnmyleswhite .

[Previous page](https://discourse.julialang.org/t/new-concrete-number-type/55210.md?page=1)
