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]> wrote:
> Wouldn't you loose type stability then?
>
> It might not matter, though.



-- 
Erik Schnetter <[email protected]>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to