There are infinitely many types for which isbits
is true. But I want to make my function dispatch on whether the eltype
is isbits
. What’s the best way to do that?
I think of 4 approaches to do that
1.Use if
Don’t use dispatch at all; just use if
-statements
Easy to understand but doesn’t use “dispatch”
2. Use Val{true|false}
fn(v) = fn(Val(isbits(eltype(v)), v)
fn(Val{True}, v) = # do something
fn(Val{False}, v) = # do something else
3. Make a new type IsBitsType
with two subtypes
which are IsBitsTrueType
and IsBitsFalseType
and make a series of function like in 2. to dispatch
4. Make a new type IsBitsType2 with 2 values
The two possible values are isbitstrue
and isbitsfalse
and the type is defined like so
struct IsBitsType2
val::Bool
and dispatch using similar mechanism as in 2.
It feels like 2
is what I would do, but keen to hear opinions on this. It sounds like 3
is very Julia specific way but I always get confused, e.g when I use the Algorithm
type when sorting, so I think I should avoid creating sub-types just for dispatch.