# Constructor with multiple parametric arguments

**URL:** https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449
**Category:** General Usage
**Tags:** parametric-types, constructors
**Created:** [September 20, 2021, 10:09am UTC](https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449 "2021-09-20T10:09:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![luchino\_96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luchino_96/32/29329_2.png) [@luchino\_96](https://discourse.julialang.org/u/luchino_96)
#### Post date: [September 20, 2021, 10:09am UTC](https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449/1 "2021-09-20T10:09:29Z")

</div>

Hi, I encounter a bizzare behaviour for an inner constructor of a struct, and I am trying to understand what is happening. The first attempt was the following

```julia
struct Log_Float{T <: Integer, S <: Number}
    sign::T ##sign of the number
    val::S ##value of the log
    
    ##INNER CONSTRUCTOR
    function Log_Float{T,S}(x::T, y::S) where T<:Integer where S<:Number
        !(x in [-1, 1]) && throw(ArgumentError("Sign has to be ether plus or minus 1")) 
        return new(x, y)
    end
    
end

```

But declaring in this form, and proceding by creating a Log\_Float, I get the following error message:

```julia
ERROR: StackOverflowError:
Stacktrace:
 [1] Main.Log_Floats.Log_Float(::Int64, ::Int64) at C:\Users\lucas\Desktop\Log_Floats.jl:17 (repeats 79984 times)

```

I tried different hands on way to solve this problem, as I created Structs with just one single parametric argument following the above structure many times, without having any problem (it’s the structure indicated on the Julia documentation). To solve the problem I had to write:

```julia
struct Log_Float{T <: Integer, S <: Number}
    sign::T ##sign of the number
    val::S ##value of the log
    
    ##INNER CONSTRUCTOR
    function Log_Float(x::T, y::S) where T<:Integer where S<:Number
        !(x in [-1, 1]) && throw(ArgumentError("Sign has to be ether plus or minus 1")) 
        return new{T,S}(x, y)
    end
    
end

```

Any idea of what was the original problem, and why the second version solves the problem?

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [September 20, 2021, 1:59pm UTC](https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449/2 "2021-09-20T13:59:44Z")

</div>

In the first case, you have recursion in the outer constructor `Log_Float(x, y)`.  
In the second case, the inner constructor method takes precedence for the recursive call.

Without seeing the outer constructor code for the first case, it’s impossible to say what exactly is wrong.

---

<div class="post-metadata">

### Author: ![luchino\_96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luchino_96/32/29329_2.png) [@luchino\_96](https://discourse.julialang.org/u/luchino_96)
#### Post date: [September 20, 2021, 2:10pm UTC](https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449/3 "2021-09-20T14:10:40Z")

</div>

First of all, thanks for the quick answer. The code for the outer constructor in the first case was:

```julia
##OUTER CONSTRUCTORS
Log_Float(x::T, y::S) where T<:Number where S<:Number = Log_Float(convert(Int64,x), y)
Log_Float(number::S) where S<:Number = Log_Float(sign(number), log(abs(number)))

```

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [September 20, 2021, 2:13pm UTC](https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449/4 "2021-09-20T14:13:01Z")

</div>

```julia
Log_Float(x::T, y::S) where T<:Number where S<:Number = Log_Float(convert(Int64,x), y)

```

Which method do you think it’s calling for the rhp?

---

<div class="post-metadata">

### Author: ![luchino\_96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luchino_96/32/29329_2.png) [@luchino\_96](https://discourse.julialang.org/u/luchino_96)
#### Post date: [September 20, 2021, 2:16pm UTC](https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449/5 "2021-09-20T14:16:17Z")

</div>

I think the right hand part should call the inner constructor. At least from my understanding.

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [September 20, 2021, 2:22pm UTC](https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449/6 "2021-09-20T14:22:25Z")

</div>

There is no inner constructor `Log_Float(x, y)`, only `Log_Float{T,S}(x, y)`.

---

<div class="post-metadata">

### Author: ![luchino\_96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luchino_96/32/29329_2.png) [@luchino\_96](https://discourse.julialang.org/u/luchino_96)
#### Post date: [September 20, 2021, 2:27pm UTC](https://discourse.julialang.org/t/constructor-with-multiple-parametric-arguments/68449/7 "2021-09-20T14:27:49Z")

</div>

Ok I see! Thank you a lot!
