# Best way to dispatch on whether a value is \`isbits\`?

**URL:** <https://discourse.julialang.org/t/best-way-to-dispatch-on-whether-a-value-is-isbits/30477>\
**Category:** New to Julia\
**Created:** [October 30, 2019, 3:55am UTC](https://discourse.julialang.org/t/best-way-to-dispatch-on-whether-a-value-is-isbits/30477 "2019-10-30T03:55:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 30, 2019, 3:55am UTC](https://discourse.julialang.org/t/best-way-to-dispatch-on-whether-a-value-is-isbits/30477/1 "2019-10-30T03:55:46Z")

</div>

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}`

```julia-auto
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

```julia-auto
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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 30, 2019, 7:10am UTC](https://discourse.julialang.org/t/best-way-to-dispatch-on-whether-a-value-is-isbits/30477/2 "2019-10-30T07:10:12Z")

</div>

> [@xiaodai](#):
>
> It feels like `2` is what I would do, but keen to hear opinions on this.

That’s pretty much the standard solution:

[https://docs.julialang.org/en/v1/manual/methods/#Trait-based-dispatch-1](https://docs.julialang.org/en/v1/manual/methods/#Trait-based-dispatch-1)

That said, when the criterion is binary, there is nothing wrong with a branch either, eg

```julia
f(v) = isbitstype(eltype(v)) ? do_something(v) : do_something_else(v)

```
