Discussion on Base.@pure usage

Thanks for the hint. I will remove @pure annotations - it was a fruitless attempt to persuade compiler to do constant propagation, leaving them in the code could cause trouble in the long run.

@pure doc states “Double check for calls to generic functions.” - their use is discouraged but not strictly forbidden in a @pure function. I am pretty sure getindex, length and reinterpret are safe to use in my code sample, because I call them only with parameters of very basic system types.

My understanding of a pure function is “no side effects, result depends only on function parameters”. Though I expect it holds for my @pure annotated functions in normal use, I cannot guarantee it. If someone defines a type MyTypeChangingGlobalState and increments a global counter in every call of bitsizeof(MyTypeChangingGlobalState), it causes side effects, and if its return value is not constant, it breaks my code.

I found this discussion clarifying dangers and benefits or @pure.

1 Like