# Get long type information for struct definition

**URL:** https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900
**Category:** Performance
**Created:** [December 16, 2024, 7:59pm UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900 "2024-12-16T19:59:13Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [December 16, 2024, 7:59pm UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/1 "2024-12-16T19:59:13Z")

</div>

Sometimes I want to wrap a struct which has long type information in another struct. For example, I want to wrap an interpolator like this one:

```julia
julia> using Interpolations

julia> cubic_spline_interpolation(0:0.1:0.2, zeros(Float64, 3)) |> typeof
Interpolations.Extrapolation{Float64, 1, ScaledInterpolation{Float64, 1, Interpolations.BSplineInterpolation{Float64, 1, OffsetArrays.OffsetVector{Float64, Vector{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}}}, BSpline{Cubic{Line{OnGrid}}}, Throw{Nothing}}

```

The type information is very long, so I like to get the type automatically instead of typing the long type information by hand.

```julia
struct Foo
itp::typeof(cubic_spline_interpolation(0:0.1:0.2, zeros(Float64, 3)))
something_else
end

```

But quite often I want a parametric type so that the interpolator can be used for real or complex numbers. Ideally, I want something like the following:

```julia
struct Foo{T}
itp::typeof(cubic_spline_interpolation(0:0.1:0.2, zeros(T, 3)))
something_else
end

```

But this definition won’t work, as `T` is just a placeholder in this definition and thus the function call `zeros(T, ...)` doesn’t work.

One solution might be

```julia
struct Foo{ITP}
itp::ITP
end

```

but I don’t like this one, as then the Struct `Foo` would have long type information.

Is there a way to solve this problem? Thanks.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [December 16, 2024, 8:33pm UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/2 "2024-12-16T20:33:14Z")

</div>

One option may be:

> **[GitHub - vtjnash/ComputedFieldTypes.jl: Build types in Julia where some fields have...](https://github.com/vtjnash/ComputedFieldTypes.jl)**
>
> Build types in Julia where some fields have computed types

I’d just go with `struct Foo{X} x::X end`, though.

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [December 16, 2024, 11:41pm UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/3 "2024-12-16T23:41:02Z")

</div>

Thank you.

At this moment, I just use two different types as those are all I need.

I really hope julia can support computing type information as the package does. As not just for the example I gave above, another common case is that the struct contains multiples collections with different lengths like N+1, N, 2N+1… This also needs to ability to compute the type information.

---

<div class="post-metadata">

### Author: ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)
#### Post date: [December 17, 2024, 1:14am UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/4 "2024-12-17T01:14:29Z")

</div>

Would this work for you?

```julia
struct Foo{T} where {T <: Interpolations.Extrapolation}
   itp::T
end

```

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [December 17, 2024, 5:55am UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/5 "2024-12-17T05:55:11Z")

</div>

I got an error.

```julia
julia> struct Foo{T} where {T <: Interpolations.Extrapolation}
          itp::T
       end
ERROR: syntax: invalid type signature around REPL[2]:1
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [December 17, 2024, 7:13am UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/6 "2024-12-17T07:13:58Z")

</div>

> [@linwaytin](#):
>
> I really hope julia can support computing type information as the package does.

That’s unfeasible because symbols can refer to different functions in different modules and a generic function can change its behavior and results dynamically, but a type and its instances’ structure _cannot_. As a consequence, those type parameters you’re trying to condense are necessary for adapting to method changes. That massive (and as far as I’m aware, internal) `Extrapolation` type is rooted in many method calls, and as far as the language is aware, any of them can change behavior and result in a different return type of `cubic_spline_interpolation`, even though nobody would do that in practice.

ComputedFieldTypes is no exception, it omits the full type parameters from the constructor methods, but they still must exist and are still printed that way. Here’s an [earlier example](https://discourse.julialang.org/t/simple-compile-time-evaluation-for-struct-types/103326/4). I’m not certain, but I think it is hypothetically possible for unchangeable and pure built-in functions like `<:` to allow very limited field type computation.

Let’s simplify your example. You effectively want to do something like:

```julia
julia> struct Foo{T}
         vec::Vector{T} # longer to type than T
       end

julia> Foo([1])
Foo{Int64}([1])

```

but with an instantiating method instead of explicitly typing the type out, like

```julia
struct Foo{T}
  vec::typeof(ones(T, 1)) # not valid code
end

```

But this is impossible because `Foo{MyType}` becomes several concrete types with different `vec` field types depending on how I redefine `Base.ones(::Type{MyType}, dims)` (don’t do this in practice). The only way for `Foo{MyType}` to be a valid type is to have `vec::Any` to accommodate all possible results of the field type computation. Going back to your example, `itp::Any` is necessary to adapt to various `cubic_spline_interpolate` calls in practice because the concrete type of `itp` also depended on the `0:0.1:0.2` input; even if we pretended methods were set in stone so parameters could be condensed, `T` for the `zeros` input was not enough. You can make a `Foo` constructor that computes `T` from `itp` before calling `Foo{T}(itp, args...)`.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [December 17, 2024, 9:29am UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/7 "2024-12-17T09:29:00Z")

</div>

> [@linwaytin](#):
>
> I really hope julia can support computing type information as the package does. As not just for the example I gave above, another common case is that the struct contains multiples collections with different lengths like N+1, N, 2N+1… This also needs to ability to compute the type information.

Used to think the same, but I don’t see this as a priority any more. Keep in mind Julia’s type system is already enormously powerful, but kinda difficult to maintain. Both bugs and performance issues exist regarding the type system and the compiler in general which should be fixed before making it yet more complicated and powerful. What you’re suggesting would be a really big project in terms of effort and risk, but with comparatively little in the way of benefits.

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [December 17, 2024, 10:33am UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/8 "2024-12-17T10:33:13Z")

</div>

Yeah, I was thinking about the template system of C++, but this may not be a good idea in general…

---

<div class="post-metadata">

### Author: ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)
#### Post date: [December 17, 2024, 2:10pm UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/9 "2024-12-17T14:10:16Z")

</div>

> [@linwaytin](#):
>
> I got an error.

Sorry, I used the wrong parametric format for `struct`s. That should have been:

```julia
struct Foo{T <: Interpolations.Extrapolation}
   itp::T
end

```

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [December 17, 2024, 4:52pm UTC](https://discourse.julialang.org/t/get-long-type-information-for-struct-definition/123900/10 "2024-12-17T16:52:43Z")

</div>

Thank you. The code works, but the type information is still long, which is what I want to avoid.
