The type Val can be used to handily associate values with constants by writing a method of a function for each pair. It’s even handier to define a function that wraps the values in Val so you don’t have to type it.
For some reason the wrapper stops constant propagation when the values are of type Integer or Symbol. When t in the snippet below is changed to nothing the propagation works and the functions bodies are just return true.
It works when adding @pure but since the compiler doesn’t recognize it on its own and the case seems to be quite simple I’m wondering if I missed something and it could be unsafe to do so. Julia version is 1.1.0.
Not sure. I thought that for Symbol this could be because isimmutable(:symbol) == false and Symbol.mutable == true. Even though Symbols are functionally immutable, the type system currently doesn’t treat them as such for certain technical reasons (see https://github.com/JuliaLang/julia/issues/30210). This probably breaks constant prop. But then I would have expected the const t = 1 variant to work, so there’s probably something else in play here.
That helped! Apparently the compiler knows that the method will return a constant result but still leaves the call in if there is no fallback method present (unless T is a fieldles struct / empty tuple).
Sadly this approach is not possible in my real code because the methods are added dynamically so that the function acts as Dict with inferable value type.