# How to use type parameter variables across multiple parametric types

**URL:** <https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162>\
**Category:** New to Julia\
**Tags:** inference, type, parametric-types\
**Created:** [November 12, 2022, 2:40pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162 "2022-11-12T14:40:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophE](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@christophE](https://discourse.julialang.org/u/christophE)\
**Post date:** [November 12, 2022, 2:40pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/1 "2022-11-12T14:40:42Z")

</div>

Hello Julia folk,

I am testing something very simple, a dot product, and I want to ensure, it gets two arrays (or vectors) of the same length. This attempt does not work:

```julia
function dot_product{N::UInt64}(vec1::Array{Float64, N}, vec2::Array{Float64, N})
	return sum((x,y) -> x * y, zip(vec1, vec2))
end

```

and neither this one:

```julia
function dot_product(vec1::Array{Float64, N}, vec2::Array{Float64, N}) where N :: UInt64
	return sum((x,y) -> x * y, zip(vec1, vec2))
end

```

(problems with the `where` clause).

Is Julia able to infer and work with types like that? How to do it?  
Thanks.

EDIT:  
As you pointed out, I accidentally wrote “length” but I meant “dimensional length” (dimensions) in my head. Sorry.

---

<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:** [November 12, 2022, 2:42pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/2 "2022-11-12T14:42:27Z")

</div>

the N parameter of an array is the number of dimensions not the length. all vectors are `Array{T,1}`

---

<div class="post-metadata">

**Author:** ![christophE](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@christophE](https://discourse.julialang.org/u/christophE)\
**Post date:** [November 12, 2022, 3:25pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/3 "2022-11-12T15:25:09Z")

</div>

Oh, thank you for the clarification. I replaced it with AbstractVector.  
But concerning this question, how can I guarantee, that parameter 1 and parameter 2 both share an Array with same number of dimensions?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 12, 2022, 3:29pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/4 "2022-11-12T15:29:03Z")

</div>

You can either use StaticArrays, or do a runtime length check.

---

<div class="post-metadata">

**Author:** ![christophE](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@christophE](https://discourse.julialang.org/u/christophE)\
**Post date:** [November 12, 2022, 3:35pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/5 "2022-11-12T15:35:31Z")

</div>

Okay, so Julia does not officially support generic variables for concrete type parameters, right?  
(For example, in languages with template parameterization, they often allow for template parameters which are constants or concrete values, not just types.)

EDIT:  
The proper functionality in my original post can be written as

```julia
function dot_product(x::AbstractVector, y::AbstractVector)
    return sum(pair -> pair[1] * pair[2], zip(x,y))
end

```

I don’t know, why Julia does not destructure a single tuple argument into multiple arguments, the `pair` thing is needed, but it gives me the same results like `·` from LinearAlgebra package.

---

<div class="post-metadata">

**Author:** ![doit](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@doit](https://discourse.julialang.org/u/doit)\
**Post date:** [November 12, 2022, 3:46pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/6 "2022-11-12T15:46:35Z")

</div>

> [@christophE](#):
>
> Okay, so Julia does not officially support generic variables for concrete type parameters, right?

What do you think the `N` stands in for?

Also, doesn’t destructing work?

```julia
function dot_product(x::AbstractVector, y::AbstractVector)
    return sum((xi,yi) -> xi * yi, zip(x,y))
end

```

Can’t check on phone.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 12, 2022, 4:02pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/7 "2022-11-12T16:02:40Z")

</div>

> [@christophE](#):
>
> Okay, so Julia does not officially support generic variables for concrete type parameters, right?

Yes, bitstype values are supported as type parameters, including integers, as you can see in `Array{Float64, 2}` for example.

But the `2` stands for number of dimensions (matrix), not length.

Also you cannot use the syntax `{N::Int}` for restricting type parameters, only `<:T` is allowed, meaning that `N` must be unrestricted.

The syntax `foo{T}(...)` for functions is also not valid, except for type constructors.

---

<div class="post-metadata">

**Author:** ![christophE](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@christophE](https://discourse.julialang.org/u/christophE)\
**Post date:** [November 12, 2022, 4:19pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/8 "2022-11-12T16:19:10Z")

</div>

I am talking about concrete values as variables for type parameters, that means `N` as an integer value, not a type. In C++ I can just write

```nohighlight
template<int N> float dot_product(std::array<float,N> x, std::array<float,N> y) ...

```

I don’t know at the moment, if conventional C++ compilers would infer the `N` automatically, but in D for example, it is able to infer it for some cases.

Compile-time variables for concrete values make a type system more flexible and better usable because you get earlier compiler warnings and don’t need to write explicit asserts or similar. (Type annotations are basically just compile-time-evaluated assertion shortcuts.)

My question only was how to do that in Julia, when supported.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 12, 2022, 4:26pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/9 "2022-11-12T16:26:56Z")

</div>

> [@christophE](#):
>
> I am talking about concrete values as variables for type parameters, that means `N` as an integer value, not a type.

Yes, that’s what I said. See for example the 2 in `Array{Float64, 2}`.

In a function signature you could use it as

```julia
foo(x::AbstractArray{Int, N}) where {N} = ... 

```

to match any dimensionality, or a specific value to restrict it.

---

<div class="post-metadata">

**Author:** ![christophE](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@christophE](https://discourse.julialang.org/u/christophE)\
**Post date:** [November 12, 2022, 4:35pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/10 "2022-11-12T16:35:52Z")

</div>

Nice! Thank you! That one worked:

```julia
function dot_product(x::AbstractArray{Float64,N}, y::AbstractArray{Float64,N}) where {N}
    ...
end

```

If I want to pass someone a reference, under what kind of term I would find documentation of this notation using `where {N}`?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 12, 2022, 4:45pm UTC](https://discourse.julialang.org/t/how-to-use-type-parameter-variables-across-multiple-parametric-types/90162/11 "2022-11-12T16:45:33Z")

</div>

Just be aware, the N does not have anything to do with size or length, only number of dimensions.

> [@christophE](#):
>
> under what kind of term I would find documentation of this notation using `where {N}`?

Under `where`: [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#where)
