# Preventing dispatch on val not bits

**URL:** <https://discourse.julialang.org/t/preventing-dispatch-on-val-not-bits/27911>\
**Category:** General Usage\
**Created:** [August 23, 2019, 10:37pm UTC](https://discourse.julialang.org/t/preventing-dispatch-on-val-not-bits/27911 "2019-08-23T22:37:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [August 23, 2019, 10:37pm UTC](https://discourse.julialang.org/t/preventing-dispatch-on-val-not-bits/27911/1 "2019-08-23T22:37:56Z")

</div>

[`Val`](https://docs.julialang.org/en/v1/base/base/#Base.Val) is mainly use to dispatch on bits type with static optimization.

It’s rather simple to do a coding error and dispatch on `Val(:foo)` that is not a bits type instance.

Is it possible that the compiler emits a warning in such case ?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 23, 2019, 11:11pm UTC](https://discourse.julialang.org/t/preventing-dispatch-on-val-not-bits/27911/2 "2019-08-23T23:11:48Z")

</div>

Are you asking about doing this in general or doing it for your code?

In general, no, it won’t be done since how you use it is definately the only way it can be used.

For you code, well, sure, just check if it’s a symbol and throw an error or print a warning.

FWIW though, this kind of warning are the most useless. If this should never be done then it should be an error and if it could be done it must not be an warning at all unless there’s a way to explicitly disable the warning **in the code**.

---

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [October 22, 2019, 2:18am UTC](https://discourse.julialang.org/t/preventing-dispatch-on-val-not-bits/27911/3 "2019-10-22T02:18:01Z")

</div>

> Typical functions all have their own types. sin is the only instance of typeof(sin); it’s a singleton. The value contains no data (sizeof(sin) == 0) and is immutable, so it’s a bits type.

Interesting clarification @ [syntax to replace `f(::typeof(+))`? · Issue #32541 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/32541#issuecomment-510179255)

In fact `Val` is not a bits type by itself

```julia
@test isbitstype(Val{T} where T) == false

```

But becomes one when is arg is specified (even with a non bits param like a symbol)

```julia
@test sizeof(Val{:a}) == 0 # has no data
@test Val{:a}() == Val{:a}() # is singleton
@test Base.unwrap_unionall(Val).mutable == false # is immutable

@test isbitstype(Val{:a})

@test isbits(:a) == false # param is not bits

```

So the compiler could statically optimize dispatch  
That has puzzled me times ago

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 22, 2019, 2:31am UTC](https://discourse.julialang.org/t/preventing-dispatch-on-val-not-bits/27911/4 "2019-10-22T02:31:09Z")

</div>

Well, I’m not sure what you are trying to say here… But a few things to clarify.

> [@o314](#):
>
> In fact `Val` is not a bits type by itself

`Val` is not a concrete type so it cannot be a bits type. In fact, anything that you can apply type arguments to aren’t bits type.

> [@o314](#):
>
> even with a non bits param like a symbol

The type parameter **never** has any effect on the isbit-ness of a type. Only the fields do. You can add unused type parameters to types and you can put whatever you want that’s allowed as type parameters in there without any effect on the isbit-ness of the type. Also note that a type (`DataType`) itself is not isbit anyway.

And finally,

> [@o314](#):
>
> So the compiler could statically optimize dispatch

Actually has nothing to do with the isbit-ness of anything.

---

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [October 23, 2019, 10:35pm UTC](https://discourse.julialang.org/t/preventing-dispatch-on-val-not-bits/27911/5 "2019-10-23T22:35:55Z")

</div>

> Actually has nothing to do with the isbit-ness of anything.

Your comment belong to you in regard to the definition of `Val` in docs

> `Val(c)`
> 
> Return `Val{c}()`, which contains no run-time data. Types like this can be used to pass the information between functions through the value `c`, which must be an isbits value. The intent of this construct is to be able to dispatch on constants directly (at compile time) without having to test the value of the constant at run time.

[https://docs.julialang.org/en/v1/base/base/#Base.Val](https://docs.julialang.org/en/v1/base/base/#Base.Val)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 23, 2019, 10:55pm UTC](https://discourse.julialang.org/t/preventing-dispatch-on-val-not-bits/27911/6 "2019-10-23T22:55:58Z")

</div>

> [@o314](#):
>
> Your comment belong to you in regard to the definition of `Val` in docs

Please read the doc string you pasted carefully. It seems that you made a lot of assumptions when reading it which is why you are confused.

> [@o314](#):
>
> Types like this can be used to pass the information between functions through the value `c` , which must be an isbits value.

This is wrong (over-simplified). As you see above, `c` does not have to be a isbit value. (Notet that here I belive the “which” is talking about `c`.) This point is related to this thread but isn’t realted to what I meant by “Actually has nothing to do with the isbit-ness of anything.”

And still, this is a specific document about `Val` and whatever it says should have no significance regarding why some properties are related to optimization or not. In particuclar, even though it happens to be a isbit type and the compiler could sometimes “statically optimize dispatch” when passing it around, the two have absolutely no relation with each other.

Also, the doc doesn’t even say the isbit-ness has anything to do with any optimization. It mentions two properties of the type and you shouldn’t assume one of them causes another. The two sentence didn’t imply that and it’s also not the case.
