I think a solution is to add another type parameter for the field type. The 
following works:

~~~
type Data{T,S}
    field::S
end

data(m)=Data{length(size(m)),typeof(m)}(m)

data(rand(10))::Data{1}
data(rand(10,10))::Data{2}
~~~

Best,

Ben



On Friday, February 19, 2016 at 1:53:36 PM UTC-5, Erik Schnetter wrote:
>
> You can keep type stability with a trick: 
>
> - Make the function I describe very small, calling only a worker 
> function that performs the actual work, and use `@inline` 
> - Use `Val{}` types in the worker function, and use dispatch instead 
> of if statements 
>
> Here's an example (untested): 
> ```Julia 
> type Impl1 end 
> type Impl2 end 
>
> @inline Impl(N) = getImpl(Val{N}) 
>
> getImpl(::Type{Val{1}}) = Impl1 
> getImpl(::Type{Val{2}}) = Impl2 
>
> ``` 
>
> The only type-unstable function is `Impl`, but if it is inlined, 
> everything is type-stable. 
>
> -erik 
>
>
>
> On Fri, Feb 19, 2016 at 1:12 PM, Davide Lasagna <[email protected] 
> <javascript:>> wrote: 
> > Wouldn't you loose type stability then? 
> > 
> > It might not matter, though. 
>
>
>
> -- 
> Erik Schnetter <[email protected] <javascript:>> 
> http://www.perimeterinstitute.ca/personal/eschnetter/ 
>

Reply via email to