Can @pure functions throw an error?

MWE, is this OK?

Base.@pure function key_in(key::Symbol, keys::Tuple{Vararg{Symbol}})
    for k in keys
        key ≡ k && return true
    end
    false
end

Base.@pure function remove_key(key::Symbol, keys::Tuple{Vararg{Symbol}})
    kept = Symbol[]
    key_in(key, keys) || error("Cannot drop $key which is not in $keys")
    for k in keys
        k ≡ key || push!(kept, k)
    end
    (kept..., )
end

remove_key(:a, (:a, :b, :c)) # (:b, :c)
remove_key(:a, (:b, :c))     # throws error

Probably a very bad idea, as pure means we can discard or duplicate side-effects, which then also means we can transform any program that might call this function into one that always throws this error at startup

4 Likes