<p dir="auto">Currently, we can perform a limited set of logic in the type definition, such as:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="type A
    a::eltype(Vector{Int})
end"><pre class="notranslate">type A
    a<span class="pl-k">::</span><span class="pl-c1">eltype</span>(Vector{Int})
<span class="pl-k">end</span></pre></div>
<p dir="auto">but we can't involve a type parameter, such as:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="type B{V &lt;: AbstractVector}
    a::eltype(V)
end"><pre class="notranslate">type B{V <span class="pl-k">&lt;:</span> <span class="pl-c1">AbstractVector</span>}
    a<span class="pl-k">::</span><span class="pl-c1">eltype</span>(V)
<span class="pl-k">end</span></pre></div>
<p dir="auto">AFAICT, at the moment the field types <code class="notranslate">A.types</code> is calculated when the type is defined and type parameters are inserted into the correct slots as they become known.</p>
<p dir="auto">However, it would be nice if the types could be calculated by arbitrary inferrable or <code class="notranslate">@pure</code> functions. Another simple example (close to my heart) would be:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="immutable StaticMatrix{M,N,T}
    data::NTuple{M*N, T}
end"><pre class="notranslate">immutable StaticMatrix{M,N,T}
    data<span class="pl-k">::</span><span class="pl-c1">NTuple{M*N, T}</span>
<span class="pl-k">end</span></pre></div>
<p dir="auto">However this results in an error that multiplication is not defined for <code class="notranslate">TypeVar</code>s. Instead, I need all of this code:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="immutable StaticMatrix{M,N,T,L}
    data::NTuple{L, T}
    function StaticMatrix(d)
        check_params(Val{L}, Val{M}, Val{N})
        new(d)
    end
end

@generated function check_params{L,M,N}(::Type{Val{L}}, ::Type{Val{M}}, ::Type{Val{N}}) # could also be `@pure` in v0.5
    if L != M*N
        error(&quot;Type parameters don't match&quot;)
    end
end"><pre class="notranslate">immutable StaticMatrix{M,N,T,L}
    data<span class="pl-k">::</span><span class="pl-c1">NTuple{L, T}</span>
    <span class="pl-k">function</span> <span class="pl-en">StaticMatrix</span>(d)
        <span class="pl-c1">check_params</span>(Val{L}, Val{M}, Val{N})
        <span class="pl-c1">new</span>(d)
    <span class="pl-k">end</span>
<span class="pl-k">end</span>

<span class="pl-c1">@generated</span> <span class="pl-k">function</span> <span class="pl-en">check_params</span><span class="pl-c1">{L,M,N}</span>(<span class="pl-k">::</span><span class="pl-c1">Type{Val{L}}</span>, <span class="pl-k">::</span><span class="pl-c1">Type{Val{M}}</span>, <span class="pl-k">::</span><span class="pl-c1">Type{Val{N}}</span>) <span class="pl-c"><span class="pl-c">#</span> could also be `@pure` in v0.5</span>
    <span class="pl-k">if</span> L <span class="pl-k">!=</span> M<span class="pl-k">*</span>N
        <span class="pl-c1">error</span>(<span class="pl-s"><span class="pl-pds">"</span>Type parameters don't match<span class="pl-pds">"</span></span>)
    <span class="pl-k">end</span>
<span class="pl-k">end</span></pre></div>
<p dir="auto">and my users need to foist around the redundant <code class="notranslate">L</code> paramater when they need to specify a concrete type.</p>
<p dir="auto">For abstract types, I'm hoping that inference itself could still be used to come up with a least-pessimistic approximation of each field, or otherwise just use <code class="notranslate">Any</code> when that's not possible. If that makes it difficult to avoid regressions, straight types (combinations of types and the relevant <code class="notranslate">TypeVar</code>s with <code class="notranslate">apply_type</code> but no other functions) could keep functioning as they currently do.</p>
<p dir="auto">Now that we have staged functions (or whatever name that will converge out of the discussion in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="36826392" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/7474" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/7474/hovercard" href="https://github.com/JuliaLang/julia/pull/7474">#7474</a>), can we be more greedy and request something similar to define types, i.e. where the type definition depends on the value of the parameters in a more general way than can currently be expressed?</p>
<p dir="auto">This would allow the easy implementation of fixed size / stack allocated arrays ( <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="37695185" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/7568" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/7568/hovercard" href="https://github.com/JuliaLang/julia/pull/7568">#7568</a> ), and the construction of <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/timholy/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/timholy">@timholy</a>  in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="43323777" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/8432" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/8432/hovercard" href="https://github.com/JuliaLang/julia/pull/8432">#8432</a> to build custom types to be used in the cartesian iterators clearly expresses the need for this. It can also have many other interesting uses. <code class="notranslate">NTuple{N,Int}</code> and <code class="notranslate">Tuple{Int,Int,Float}</code> (in the notation of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="43838745" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/8470" data-hovercard-type="issue" data-hovercard-url="/JuliaLang/julia/issues/8470/hovercard" href="https://github.com/JuliaLang/julia/issues/8470">#8470</a> ) could be special cases (although I guess there are technical reasons that they need to be implemented separately?).</p>