# Why does this constructor not work with 1 Int parameter?

**URL:** <https://discourse.julialang.org/t/why-does-this-constructor-not-work-with-1-int-parameter/49585>\
**Category:** New to Julia\
**Created:** [November 4, 2020, 2:06pm UTC](https://discourse.julialang.org/t/why-does-this-constructor-not-work-with-1-int-parameter/49585 "2020-11-04T14:06:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [November 4, 2020, 2:06pm UTC](https://discourse.julialang.org/t/why-does-this-constructor-not-work-with-1-int-parameter/49585/1 "2020-11-04T14:06:41Z")

</div>

Hi all,

I am new to Julia and am experimenting with trying to create compact code. I created the following types:

```julia
module Types

export Percentage

struct Percentage <: Real
    value::Float64
    Percentage(x) = x < 0 ? 0 : x > 1 ? 1 : new(round(x, digits = 6))
end

Base.show(io::IO, x::Percentage) = print(io, "$(x.value * 100)%")

Base.convert(::Type{Percentage}, x::Real) = Percentage(x)
Base.convert(::Type{Percentage}, x::Percentage) = x

Base.promote_rule(::Type{T}, ::Type{Percentage}) where T <: Real = Percentage

import Base: +, -, <, >, <=, >=, ==

+(x::Percentage, y::Percentage) = Percentage(x.value + y.value)
-(x::Percentage) = Percentage(-x.value)
-(x::Percentage, y::Percentage) = Percentage(x.value - y.value)
<(x::Percentage, y::Percentage) = x.value < y.value
<=(x::Percentage, y::Percentage) = x.value <= y.value
>(x::Percentage, y::Percentage) = x.value > y.value
>=(x::Percentage, y::Percentage) = x.value >= y.value
==(x::Percentage, y::Percentage) = x.value == y.value

Base.max(x::Percentage, y::Percentage) = Percentage(max(x.value, y.value))
Base.min(x::Percentage, y::Percentage) = Percentage(min(x.value, y.value))

end

abstract type Longevity end

struct Mechanical <: Longevity
    health::Percentage
    damage_thresholds::Vector{Tuple{Percentage, Float64}}
    repair_thresholds::Vector{Tuple{Percentage, Float64}}
end

Mechanical(
    health::Real = 1;
    damage_thresholds::Vector{Tuple{Percentage,Real}} = Vector(),
    repair_thresholds::Vector{Tuple{Percentage,Real}} = Vector(),
) = Mechanical(Percentage(health), damage_thresholds, repair_thresholds)

```

I tried to use the Mechanical constructor with an Int  
`Mechanical(1)`  
expecting that the int would call the Percentage() constructor and the rest of the parameters would be set to their default values. Instead I get the following error:

```julia
ERROR: MethodError: no method matching #Mechanical#10(::Array{Any,1}, ::Array{Any,1}, ::Type{Mechanical}, ::Int64)
Closest candidates are:
  #Mechanical#10(::Array{Tuple{Percentage,Real},1}, ::Array{Tuple{Percentage,Real},1}, ::Type{Mechanical}, ::Real) at /Users/stef/Programming/Julia Projects/Simulation Cockpit/cockpit/Entities.jl:35
Stacktrace:
 [1] Mechanical(::Int64) at /Users/stef/Programming/Julia Projects/Simulation Cockpit/cockpit/Entities.jl:35
 [2] top-level scope at none:1

```

I’m completely confused with the suggested constructor since the order of the parameters looks completely out of whack. I guess I’m missing something but I don’t know what to be honest.

Thanks in advance,  
Stef

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [November 4, 2020, 2:32pm UTC](https://discourse.julialang.org/t/why-does-this-constructor-not-work-with-1-int-parameter/49585/2 "2020-11-04T14:32:37Z")

</div>

Welcome to the community! Hope you’ll like Julia as we all do 🙂

I do not know, whether it helps the original problem, but this is bad

```julia
julia> Percentage(-1)
0

julia> Percentage(2)
1

julia> Percentage(0.5)
Percentage(0.5)

```

The definition of `Percentage` should be fixed to something like

```julia
    Percentage(x) = x < 0 ? new(0) : x > 1 ? new(1) : new(round(x, digits = 6))

```

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [November 4, 2020, 2:36pm UTC](https://discourse.julialang.org/t/why-does-this-constructor-not-work-with-1-int-parameter/49585/3 "2020-11-04T14:36:06Z")

</div>

the error here is due to:

```julia
julia> Vector() isa Vector{Tuple{Percentage, Real}}
false

julia> typeof(Vector()) === Vector{Any}
true

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [November 4, 2020, 2:41pm UTC](https://discourse.julialang.org/t/why-does-this-constructor-not-work-with-1-int-parameter/49585/4 "2020-11-04T14:41:19Z")

</div>

Problem can be solved by removing restrictions in `Mechanical` (there is no need in them anyway)

```julia
Mechanical(
           health::Real = 1;
           damage_thresholds = [],
           repair_thresholds = []
) = Mechanical(Percentage(health), damage_thresholds, repair_thresholds)

julia> Mechanical(1)
Mechanical(Percentage(1.0), Tuple{Percentage,Float64}[], Tuple{Percentage,Float64}[])

```

Rule of thumb: remove arguments restrictions in functions and introduce them only it is really necessary.

---

<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:** [November 4, 2020, 3:59pm UTC](https://discourse.julialang.org/t/why-does-this-constructor-not-work-with-1-int-parameter/49585/5 "2020-11-04T15:59:19Z")

</div>

> [@Skoffer](#):
>
> The definition of `Percentage` should be fixed to something like

Cf `clamp`.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [November 5, 2020, 9:27am UTC](https://discourse.julialang.org/t/why-does-this-constructor-not-work-with-1-int-parameter/49585/6 "2020-11-05T09:27:42Z")

</div>

Thank you!

I’m growing more and more fond of it 🙂

Thanks for pointing out the error.

Kind regards,  
Stef
