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