On Thu, 2015-05-21 at 16:44, Josh Langsfeld <[email protected]> wrote:
> Thanks, creating an IntParam or IntVal type does seem like the best option 
> if the type constraint is not available.
>
> Another interesting workaround I came across is:
>
> f{N}(::Type{Val{N}}, ::Type{Int}=typeof(N)) = N
>
> which will throw a no-method error if you call it with non-Ints. Of course 
> you can get around it easily by just passing Int as the second argument but 
> at least there's some automated checking.

After seeing this example I checked the generated code and it is
efficient: the check is removed for Int arguments.  However, this
optimisation actually works just as well for this:

julia> g{N}(::Type{Val{N}}) = (N::Int; N)
g (generic function with 1 method)

julia> @code_native g(Val{4})
      .text
Filename: none
Source line: 1
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $4, %eax
Source line: 1
        popq    %rbp
        ret

which is more readable.

> On Thursday, May 21, 2015 at 6:30:05 AM UTC-4, Simon Danisch wrote:
>>
>> So f will look like this now:
>> *f{N}(x::IntParam{N}*) instead of *f{N}(x::Type{IntParam{N}}).*
>>
>> Am Mittwoch, 20. Mai 2015 23:28:26 UTC+2 schrieb Josh Langsfeld:
>>>
>>> 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