# Int / Float type inference in constructor

**URL:** <https://discourse.julialang.org/t/int-float-type-inference-in-constructor/62395>\
**Category:** General Usage\
**Created:** [June 4, 2021, 3:56pm UTC](https://discourse.julialang.org/t/int-float-type-inference-in-constructor/62395 "2021-06-04T15:56:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [June 4, 2021, 3:56pm UTC](https://discourse.julialang.org/t/int-float-type-inference-in-constructor/62395/1 "2021-06-04T15:56:00Z")

</div>

I defined a struct as follows:

```julia
struct inverse{T<:Number}
x::Array{T}
y::Array{T}
function inverse{T}(x0::Tuple{...,T,...}) where T<:Number
x = convert_to_x(x0)
y = inv(x)
new(promote(x,y)...)
end
end

```

where the constructor is incomplete. The idea is that `y=inv(x)`, both are square matrices with the same `eltype`. The input is some nested tuple that contains column vectors (or tuples), I first convert it to a matrix then take the inverse. The problem is when x0 is `Int`-valued, x becomes `Int`-valued, and y is `float`-valued. I can attempt to promote `(x,y)` to float-valued, but the difficulty is the type `T` will be inferenced as `Int`, so the struct object still contains `Int`-valued arrays. How am I supposed to force the promotion without knowing a-priori the promoted type `T`?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 4, 2021, 4:12pm UTC](https://discourse.julialang.org/t/int-float-type-inference-in-constructor/62395/2 "2021-06-04T16:12:49Z")

</div>

Does it have to be an inner constructor? If not, there is no problem, I think.

```julia
function inverse(x0)
  x = [...]
  y = [...]
  inverse(promote(x,y)...)
end

```

---

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [June 4, 2021, 4:17pm UTC](https://discourse.julialang.org/t/int-float-type-inference-in-constructor/62395/3 "2021-06-04T16:17:53Z")

</div>

Thanks. I had problems with outer constructor. It looks like if this struct is nested in another struct as a field, only the inner constructor is called when the nesting struct is created, in which case I get an error complaining “no matching method for blah blah …” even though that particular method is defined as an outer constructor and works from the REPL.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 4, 2021, 4:41pm UTC](https://discourse.julialang.org/t/int-float-type-inference-in-constructor/62395/4 "2021-06-04T16:41:46Z")

</div>

This is mostly unrelated to your question, but you want `Matrix{T}` not `Array{T}`

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 4, 2021, 5:06pm UTC](https://discourse.julialang.org/t/int-float-type-inference-in-constructor/62395/5 "2021-06-04T17:06:19Z")

</div>

Why do you want this? I’m very curious to hear a good reason for a numerical matrix inverse.

> [@Non-allocating matrix inversion](https://discourse.julialang.org/t/non-allocating-matrix-inversion/62264/8):
>
> using LinearAlgebra, BenchmarkTools, BenchmarkPlots, StatsPlots bg = BenchmarkGroup() bg[:inv] = @benchmarkable A\*inv(B)\*b setup=(A=rand(100,100);B=rand(100,100);b=rand(100)) bg[:backslash] = @benchmarkable A\*(B\b) setup=(A=rand(100,100);B=rand(100,100);b=rand(100)) bg[:slash] = @benchmarkable (A/B)\*b setup=(A=rand(100,100);B=rand(100,100);b=rand(100)) bg[:qr] = @benchmarkable A\*(qr(B)\b) setup=(A=rand(100,100);B=rand(100,100);b=rand(100)) bg[:lu] = @benchmarkable A\*(lu(B)\b) setup=(A=rand(100,1…

---

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [June 4, 2021, 6:16pm UTC](https://discourse.julialang.org/t/int-float-type-inference-in-constructor/62395/6 "2021-06-04T18:16:13Z")

</div>

You are right. Mostly we don’t want to explicitly compute matrix inverse. My situation is that those are 2x2 `SMatrix` which are used frequently. It makes the code look cleaner w/o much affecting performance.
