# Dispatching on values with constraints

**URL:** <https://discourse.julialang.org/t/dispatching-on-values-with-constraints/116716>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [July 7, 2024, 9:22am UTC](https://discourse.julialang.org/t/dispatching-on-values-with-constraints/116716 "2024-07-07T09:22:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [July 7, 2024, 9:22am UTC](https://discourse.julialang.org/t/dispatching-on-values-with-constraints/116716/1 "2024-07-07T09:22:29Z")

</div>

Within a `where` expression, I can create upper bounds on types with `<:`

```julia
Array{T} where {T<:Real}

```

I am just wondering if it will ever be possible to use `::` or `isa` within `where` expressions to dispatch on type parameter values, like

```julia
Val{T} where {T::Real}
# or, perhaps
Val{T} where {T isa Real}

```

so that, e.g., `Val(1) <: Val{T isa Real}`. I suppose it just might not ever be possible with the type system and I probably need to use WhereTraits.jl / SimpleTraits.jl. (Traits would also be nice for things like `f(::Array{T,N}) where {T,N<=2} = ...` without needing both `Array{T,1}` and `Array{T,2}`)

_Aside:_  
Part of the reason I ask is because types are technically instances of a class `DataType`:

```julia
julia> typeof(Float64)
DataType

```

So, in some ways, `T <: Real` isn’t too far removed from `x < 1.0`. Technically, `<:` is just an operator between two instances `T` and `Real`. Obviously on the practical and implementation side, these are very different! But it’s interesting to think about this more generally as `where` clauses describing a subset of some space, and the dispatch system targetting the method with the smallest volume(/measure) which contains the instance. Right now we are working with a 1D measures along `DataType` space but perhaps this could be extended to other spaces if there is a hierarchy in place so that smaller measure along one axis takes precedent over a smaller measure along another axis.

Probably also relevant to multiple inheritance: [abstract multiple inheritance · Issue #5 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/5)

Related:

- [Modifying TypeVar that will have a concrete value?](https://discourse.julialang.org/t/modifying-typevar-that-will-have-a-concrete-value/106149)
- [GitHub - vtjnash/ComputedFieldTypes.jl: Build types in Julia where some fields have computed types](https://github.com/vtjnash/ComputedFieldTypes.jl)

---

<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:** [July 7, 2024, 2:20pm UTC](https://discourse.julialang.org/t/dispatching-on-values-with-constraints/116716/2 "2024-07-07T14:20:39Z")

</div>

> <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
> \`\`\`

NB: the discussion in that PR is not completely on-topic relatively to the original issue post. The issue was IMO hijacked from the original topic, but in any case the current title and topic corresponds to your question.

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [July 7, 2024, 3:33pm UTC](https://discourse.julialang.org/t/dispatching-on-values-with-constraints/116716/3 "2024-07-07T15:33:54Z")

</div>

Cool! Looks like it’s on the road map which is great. Thanks for the link.
