# Is it possible to express a type relationship between arrays with different numbers of dimensions?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-express-a-type-relationship-between-arrays-with-different-numbers-of-dimensions/30149
**Category:** General Usage
**Tags:** question
**Created:** [October 22, 2019, 12:22am UTC](https://discourse.julialang.org/t/is-it-possible-to-express-a-type-relationship-between-arrays-with-different-numbers-of-dimensions/30149 "2019-10-22T00:22:10Z")
**Posts on this page:** 2
**Page:** 2

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 22, 2019, 4:57pm UTC](https://discourse.julialang.org/t/is-it-possible-to-express-a-type-relationship-between-arrays-with-different-numbers-of-dimensions/30149/21 "2019-10-22T16:57:09Z")

</div>

> [@bcmichael](#):
>
> Julia enforces those restrictions on other types, but for value types I have to enforce those restrictions manually in my code. That doesn’t feel elegant to me.

I agree — the biggest inelegance IMO is the fact that declaring `struct Foo{X <: Any}` actually supports using things that _aren’t_ subtype of `Any`!

```Julia
julia> struct Foo{X<:Any} end

julia> Foo{1}()
Foo{1}()

julia> struct Bar{X<:Real} end

julia> Bar{1}()
ERROR: TypeError: in Bar, in X, expected X<:Real, got Int64
Stacktrace:
 [1] top-level scope at REPL[50]:1

```

This is of course necessitated by the fact that `Foo{X}` is the same as `Foo{X<:Any}`, but it is indeed a wart. It’d be great to support `isa` sorts of constraints directly some day. It’s a “bug”, but not a very high priority.

> <https://github.com/JuliaLang/julia/issues/9580>
>
> When a type parameter is specified to be \`T \<: Top\`, it actually accept non-type… as parameters as well.
> 
> \`\`\` julia
> julia\> type A{T\<:Top}
> end
> 
> julia\> A{1}
> A{1}
> 
> julia\> A{1.2}
> A{1.2}
> \`\`\`
> 
> Although \`1\` and \`1.2\` are clearly not subtypes of \`Top\`
> 
> \`\`\` julia
> julia\> 1 \<: Top
> ERROR: type: subtype: expected Type{T\<:Top}, got Int64
> 
> julia\> 1.2 \<: Top
> ERROR: type: subtype: expected Type{T\<:Top}, got Float64
> \`\`\`
> 
> This is also the case for the builtin \`Type\` (some other types like \`Array\` doesn't even have this constraint specified)
> 
> \`\`\` julia
> julia\> Type
> Type{T\<:Top}
> 
> julia\> Type{1}
> Type{1}
> 
> julia\> Type{1.2}
> Type{1.2}
> \`\`\`
> 
> No error is raised even if this parameter is used as a type of a field
> 
> \`\`\` julia
> julia\> type A{T\<:Top}
> a::T
> end
> 
> julia\> A{1}
> A{1}
> 
> julia\> A{1}.names
> (:a,)
> 
> julia\> A{1}.types
> (1,)
> \`\`\`
> 
> The same thing happens for \`T \<: Any\` although this time it correctly reject \`Core.Unref\`
> 
> \`\`\` julia
> julia\> type A{T \<: Any}
> end
> 
> julia\> A{1}
> A{1}
> 
> julia\> A{1.2}
> A{1.2}
> 
> julia\> A{Core.Undef}
> ERROR: type: A: in T, expected T, got Type{Undef}
> 
> julia\> Core.Undef \<: Any
> false
> \`\`\`
> 
> Specifying other types seems fine
> 
> \`\`\` julia
> julia\> type C{T \<: Integer}
> end
> 
> julia\> C{1}
> ERROR: type: C: in T, expected T\<:Integer, got Int64
> 
> julia\> C{Int}
> C{Int64}
> \`\`\`
> 
> Another related issue is that \`Vararg\` doesn't specify any constraint on the parameter and therefore the following syntax is allowed.
> 
> \`\`\` julia
> julia\> (1...)\[1\]
> 1...
> 
> julia\> typeof((1...)\[1\])
> DataType
> \`\`\`

In any case, check out Tamas’ PR again — I added a commit there (before I even saw your comment here) that addresses your comment a bit more directly.

---

<div class="post-metadata">

### Author: ![bcmichael](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcmichael/32/10003_2.png) [@bcmichael](https://discourse.julialang.org/u/bcmichael)
#### Post date: [October 22, 2019, 5:12pm UTC](https://discourse.julialang.org/t/is-it-possible-to-express-a-type-relationship-between-arrays-with-different-numbers-of-dimensions/30149/22 "2019-10-22T17:12:12Z")

</div>

I guess that might help out somewhat with what I was asking about. It would definitely be more convenient if the hidden parameters could more deeply buried, so that it’s much less necessary to ever have to worry about their existence. I guess some of that has been discussed before, but with that solution you do still need to worry about the extra type parameters whenever you need to specify a concrete type (building a collection or using it as a field in another struct). Maybe I’m wrong, but it sounds like this would probably have to be baked into the language for avoiding that to really be possible.  
[https://github.com/JuliaLang/julia/issues/18466](https://github.com/JuliaLang/julia/issues/18466)

[Previous page](https://discourse.julialang.org/t/is-it-possible-to-express-a-type-relationship-between-arrays-with-different-numbers-of-dimensions/30149.md?page=1)
