> Ideally, I would like to write
>
> func{N::Int}(::Type{Val{N}) = N
>
> and get no-method errors if N is a float, symbol, etc... Has there been 
> previous discussion on this topic?

I'm pretty sure this is not possible and yes, I've seen previous
discussions on this but not sure where.

How about:

julia> func(::Type{Val{0}}) = 1
func (generic function with 4 methods)

julia> func{V<:Val}(::Type{V}) = 2
func (generic function with 4 methods)

julia> func(Val{0})
1

julia> func(Val{7})
2

julia> func(Val{Float64})
2


I also tried this, which would be a bit closer to your requirement:

julia> f(::Val{0}) = 1
f (generic function with 1 method)

julia> f(::Val) = 2
f (generic function with 2 methods)

julia> f{T<:Any}(::Val{T}) = error("Need value parameter")
f (generic function with 3 methods)

julia> f(Val{0}())
1

julia> f(Val{1}())
ERROR: Need Integer parameter
 in f at none:1

But it doesn't work.  And I think, this is a bug as

julia> 1<:Any
ERROR: TypeError: subtype: expected Type{T}, got Int64

> On Wednesday, May 20, 2015 at 5:28:26 PM UTC-4, Josh Langsfeld wrote:
>>
>> I want to implement some functionality in multiple methods and have the 
>> dispatch controlled by an Int variable "N". The trick is I want one method 
>> to be called if N == 0 and another one to be called for all other values of 
>> N. Is there a way I can do this with "Val{N}" without making the method 
>> applicable to everything? That is, can I write a generic method 
>> "func(::Val{N}) and constrain N to be an Int only?
>>

Reply via email to