# Manipulating Parameters of Parametrized Types

**URL:** <https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763>\
**Category:** General Usage\
**Tags:** question, parametric-types\
**Created:** [September 25, 2021, 10:45pm UTC](https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763 "2021-09-25T22:45:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Slagle](https://avatars.discourse-cdn.com/v4/letter/i/b5a626/32.png) [@Ian\_Slagle](https://discourse.julialang.org/u/Ian_Slagle)\
**Post date:** [September 25, 2021, 10:45pm UTC](https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763/1 "2021-09-25T22:45:53Z")

</div>

I have a struct which is composed of N components and N - 1 weights (because they add up to 1 and I don’t need the additional degree of freedom). My attempt is below

```julia
struct WeightedComponents{N}
    components::NTuple{N, Component}
    weights::NTuple{N - 1, Float64}
end

```

While this works for just N for both, the subtraction causes issues (probably because it doesn’t know that I’ll only use integers here. I also would be totally okay with something like this:

```julia
struct WeightedComponents
    N::Int64
    components::NTuple{N, Component}
    weights::NTuple{N - 1, Float64}
end

```

but that triggers an UndefVarError for N.

How should I go about making this type?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 25, 2021, 10:47pm UTC](https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763/2 "2021-09-25T22:47:48Z")

</div>

my suggestion would be make the struct:

```julia
struct WeightedComponents{N, M}
    components::NTuple{N, Component}
    weights::NTuple{M, Float64}
end

```

and enforce `M == N-1` and `sum(weights)==1` in your inner constructor.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 26, 2021, 1:08am UTC](https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763/3 "2021-09-26T01:08:47Z")

</div>

I would probably just make `components` a `Vector{Component}` and `weights` a `Vector{Float64}`. A tuple is not really the right container for a large homogeneous collection of values. If `N` is large you will tax the compiler, since it will be keeping track of `N` different types. Although I wonder if the compiler has optimizations for `NTuple`, compared to tuples with arbitrary types for each element…

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [September 26, 2021, 3:03am UTC](https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763/4 "2021-09-26T03:03:14Z")

</div>

I’d still be interested to know how to answer the question as stated.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 26, 2021, 3:14am UTC](https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763/5 "2021-09-26T03:14:31Z")

</div>

the answer is you can’t

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 26, 2021, 1:14pm UTC](https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763/6 "2021-09-26T13:14:41Z")

</div>

I think in order to encode that kind of thing in the type system you need dependent types, which very few languages have.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [September 26, 2021, 7:04pm UTC](https://discourse.julialang.org/t/manipulating-parameters-of-parametrized-types/68763/7 "2021-09-26T19:04:26Z")

</div>

Looks like this package can do it.

[https://github.com/vtjnash/ComputedFieldTypes.jl](https://github.com/vtjnash/ComputedFieldTypes.jl)
