Not sure what you’re asking, but if you want the *why* of this question, 
the reason is that I have an interface (of Interpolations.jl) that 
currently takes types (i.e. the argument type is Type{T}) as configuration 
flags. For flexibility, I want to switch to instances 
<https://github.com/tlycken/Interpolations.jl/pull/74> (i.e. argument type T), 
but at least for a while I also want existing functionality to be backwards 
compatible. To do that, I want to create forwards of the type

foo{T}(::Type{T}) = foo(T())

but Tuple{S,T}() isn’t possible for (and the flags can be combined, 
recursively, in tuples) so I needed a recursive solution to instantiate the 
tuples. construct_instance solves this problem

julia> construct_instance(Tuple{S,T})
(S(), T())
julia> construct_instance(Tuple{S,Tuple{T, U, V}})
(S(), (T(), U(), V()))

This lets me get away with

foo(::Type{T}) = foo(construct_instance(T))
function foo(::T)
    # actual implementation
end

and keep exposing both methods in the API.

// T

On Tuesday, October 6, 2015 at 9:15:32 PM UTC+2, Jeffrey Sarnoff wrote:

Would you show a simple example of the use, and defs for tuple, ntuple 
> below?
>
> On Monday, October 5, 2015 at 5:42:17 AM UTC-4, Tomas Lycken wrote:
>>
>> Ha! It seems all I needed was a type-assertion at the end. Both of these 
>> work the way I want them to:
>>
>> construct_instance{T<:Tuple}(::Type{T}) = tuple([construct_instance(t) for t 
>> in T.parameters]...)::T
>> construct_instance{T<:Tuple}(::Type{T}) = ntuple(i -> 
>> construct_instance(T.parameters[i]), length(T.parameters))::T
>>
>> // T
>>
>> On Monday, October 5, 2015 at 11:33:25 AM UTC+2, Tomas Lycken wrote:
>>
>> I managed to get rid of the list comprehension by using ntuple:
>>>
>>> construct_instance{T<:Tuple}(::Type{T}) = ntuple(i -> 
>>> construct_instance(T.parameters[i]), length(T.parameters))
>>>
>>> However, this suffers from the same instability problem (because of the 
>>> anonymous function?). I’ll keep trying, but I would gladly accept any 
>>> advice on how to do this.
>>>
>>> // T
>>>
>>> On Monday, October 5, 2015 at 10:28:15 AM UTC+2, Tomas Lycken wrote:
>>>
>>> I’ve tried to do the following
>>>>
>>>> construct_instance{T}(::Type{T}) = T()
>>>> construct_instance{T<:Tuple}(::Type{T}) = tuple([construct_instance(t) for 
>>>> t in T.parameters]...)
>>>>
>>>> as a way to dynamically and recursively create an instance of a tuple 
>>>> type. The tuple type might be nested, it might be any length > 0, and all 
>>>> (supported) non-tuple types in the hierarchy are leaf-types with an empty 
>>>> constructor defined.
>>>>
>>>> This works insofar as it gives me the tuple I want back, but it’s not 
>>>> type stable; the inferred return type is Tuple rather than T. I also 
>>>> tried wrapping it in convert(T, tuple(...)), but that only gave me 
>>>> e.g. Tuple{Any, Any} for a two-tuple type, which I assume is because 
>>>> type info is lost in the list comprehension.
>>>>
>>>> Is there a way to help type inference realize that it will get a T 
>>>> back, even when T is a tuple type?
>>>>
>>>> // T
>>>> ​
>>>>
>>> ​
>>>
>> ​
>>
> ​

Reply via email to