On Tue, Aug 23, 2016 at 4:46 AM, Davide Lasagna <[email protected]>
wrote:

> Hi,
>
> Why do i need to specify all the parameters in the signature of methods
> that take `::Type` arguments?
>
> Demo:
> # define some parametric type
> julia> immutable foo{T, N, P, S}
>        end
>
> # this will not work, but it seems natural to omit trailing unused
> parameters
> julia> bar{T}(::Type{foo{T}}) = T
> bar (generic function with 1 method)
>
> # yes, it does not work
> julia> bar(foo{1, 2, 3, 4})
> ERROR: MethodError: `bar` has no method matching bar(::Type{foo{1,2,3,4}})
>
> # this will do the job,
> julia> bar2{T, N, P, S}(::Type{foo{T, N, P, S}}) = T
> bar2 (generic function with 1 method)
>
> # yep
> julia> bar2(foo{1, 2, 3, 4})
> 1
>
>
>
If you don't mind also matching foo, foo{1}, foo{1,2}, foo{1,2,3} in
additional to foo{1,2,3,4} and don't need to access any type parameters,
you could use `bar{T<:foo}(::Type{T})`
Not necessarily applicable in this case.


> In methods that take instances of a type it is usually not necessary to
> specify trailing unused parameter, meaning that, for instance
> baz{T}(f::foo{T}) = one(T)
> will work for any values of `N`, `P` and `S`.
>
> Thanks!
>
> P.S. This is on 0.4
>
>
>
>

Reply via email to