# Parametric subtyping, StackOverflowError issues

**URL:** <https://discourse.julialang.org/t/parametric-subtyping-stackoverflowerror-issues/24750>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [May 29, 2019, 9:11pm UTC](https://discourse.julialang.org/t/parametric-subtyping-stackoverflowerror-issues/24750 "2019-05-29T21:11:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 29, 2019, 9:11pm UTC](https://discourse.julialang.org/t/parametric-subtyping-stackoverflowerror-issues/24750/1 "2019-05-29T21:11:17Z")

</div>

I think I don’t do it right, although it works (more or less)… Given a

```julia
using StaticArrays

struct Position{T} <: FieldVector{3, T}
    x::T
    y::T
    z::T
end

```

I can create and calculate nicely with the type:

```julia
> Position(1,2,3)
3-element Position{Int64}:
 1
 2
 3

```

but I get a `StackOverflowError` when I pass values with differing types:

```julia
> Position(1.0,2,3)
StackOverflowError:

Stacktrace:
 [1] Position(::Float64, ::Vararg{Any,N} where N) at /home/tgal/.julia/packages/StaticArrays/VyRz3/src/convert.jl:4
 [2] Position(::Tuple{Float64,Int64,Int64}) at /home/tgal/.julia/packages/StaticArrays/VyRz3/src/FieldVector.jl:20
 ... (the last 2 lines are repeated 9988 more times)
 [19979] Position(::Float64, ::Vararg{Any,N} where N) at /home/tgal/.julia/packages/StaticArrays/VyRz3/src/convert.jl:4

```

I am not sure how to do it correctly so that the type conversion is made automatically. I also tried to somehow bring in a `where T<:Real`, but no success with the syntax yet, also I am not sure if it would even help 😉

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 29, 2019, 10:05pm UTC](https://discourse.julialang.org/t/parametric-subtyping-stackoverflowerror-issues/24750/2 "2019-05-29T22:05:24Z")

</div>

```julia
struct Position{T} <: FieldVector{3, T}
    x::T
    y::T
    z::T
end

```

says that `x`, `y`, `z` share the same type (`T`, whatever that happens to be)  
so `Position(1.0, 1, 1//1)` is not going to work without some intermediation.

```julia
Position(x,y,z) = Position(promote(x,y,z)...)

```

now it works because the types are made uniform

```julia
julia> Position(1.0, 1, 1//1)
3-element Position{Float64} with indices SOneTo(3):
 1.0
 1.0
 1.0

```

For best throughput, you could define an inner constructor that is used when the types of `x`, `y`, `z` are given the same, and the outer constructor as shown above for all other cases (mixed types).

```julia
struct Position{T} <: FieldVector{3, T}
    x::T
    y::T
    z::T

    Position(x::T, y::T, z::T) where {T} = new{T}(x, y, z)
end

Position(x, y, z) = Position(promote(x, y, z)...)

```

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 29, 2019, 10:12pm UTC](https://discourse.julialang.org/t/parametric-subtyping-stackoverflowerror-issues/24750/3 "2019-05-29T22:12:36Z")

</div>

Awesome, thanks 🙂
