I saw your repost on julia-dev, but replying here.

m2 won't work because it expects a series of tuple arguments (which if 
supplied would slurp up into a tuple of tuples).

m3 seems the way to go. 

I wouldn't necessarily look at it as indirect however.
Think of it as one function with 2 methods: one version is for a single 
tuple argument, the other is for the equivalent expressed as 
multiple arguments.
Also, you can define the 2 versions as m3 rather than m3 and _m3.

In fact you could use dispatch to add error processing with a third method:

module Foos
    type Foo{T <: Tuple}
    end
    m4{T<:Tuple}(f::Foo{T}, index::Tuple) = error("expected index type $T, 
got $(typeof(index))")
    m4{T<:Tuple}(f::Foo{T}, index::T) = 42
    m4(f::Foo, index...) = m4(f, index)
end

f1 = Foos.Foo{Tuple{Int}}()
Foos.m4(f1, 9)

f2 = Foos.Foo{Tuple{Int,Symbol}}()
Foos.m4(f2, (9, :a))
Foos.m4(f2, 9, :a)

Foos.m4(f2, (9.0, :a))  # error, wrong types
Foos.m4(f2, 9.0, :a)    # error, wrong types



On Thursday, March 31, 2016 at 1:41:56 AM UTC+11, Tamas Papp wrote:

> Hi Bill, 
>
> It works for a single argument, but not for multiple ones. I have a 
> self-contained example: 
>
> --8<---------------cut here---------------start------------->8--- 
> module Foos 
>
> type Foo{T <: Tuple} 
> end 
>
> m1{T}(f::Foo{Tuple{T}}, index::T...) = 42 # suggested by Bill Hart 
>
> m2{T}(f::Foo{T}, index::T...) = 42 # what I thought would work 
>
> _m3{T}(f::Foo{T}, index::T) = 42 # indirect solution 
> m3{T}(f::Foo{T}, index...) = _m3(f, index) 
>
> end 
>
> f1 = Foos.Foo{Tuple{Int}}() 
>
> Foos.m1(f1, 9)                  # works 
> Foos.m2(f1, 9)                  # won't work 
> Foos.m3(f1, 9)                  # works 
>
> f2 = Foos.Foo{Tuple{Int,Symbol}}() 
>
> Foos.m1(f2, 9, :a)               # won't work 
> Foos.m2(f2, 9, :a)               # won't work 
> Foos.m3(f2, 9, :a)               # indirect, works 
> --8<---------------cut here---------------end--------------->8--- 
>
> For more than one argument, only the indirect solution works. 
>
> Best, 
>
> Tamas 
>
> On Wed, Mar 23 2016, via julia-users wrote: 
>
> > The following seems to work, but I'm not sure whether it was what you 
> > wanted: 
> > 
> > import Base.getindex 
> > 
> > type Foo{T <: Tuple} 
> > end 
> > 
> > getindex{T}(f::Foo{Tuple{T}}, index::T...) = 42 
> > 
> > Foo{Tuple{Int}}()[9] 
> > 
> > Bill. 
> > 
> > On Wednesday, 23 March 2016 14:38:20 UTC+1, Tamas Papp wrote: 
> >> 
> >> Hi, 
> >> 
> >> My understanding was that ... in a method argument forms a tuple, but I 
> >> don't know how to dispatch on that. Self-contained example: 
> >> 
> >> import Base.getindex 
> >> 
> >> type Foo{T <: Tuple} 
> >> end 
> >> 
> >> getindex{T}(f::Foo{T}, index::T...) = 42 
> >> 
> >> Foo{Tuple{Int}}()[9] ## won't match 
> >> 
> >> ## workaround with wrapper: 
> >> 
> >> _mygetindex{T}(f::Foo{T}, index::T) = 42 
> >> mygetindex{T}(f::Foo{T}, index...) = _mygetindex(f, index) 
> >> 
> >> mygetindex(Foo{Tuple{Int}}(), 9) 
> >> 
> >> Is it possible to make it work without a wrapper in current Julia? 
> >> 
> >> Best, 
> >> 
> >> Tamas 
> >> 
>
>

Reply via email to