SymbolicML / DynamicExpressions.jl

Ridiculously fast symbolic expressions
https://symbolicml.org/DynamicExpressions.jl/dev
Apache License 2.0
90 stars 11 forks source link

Consider a different way to error on extension not loaded #68

Open avik-pal opened 2 months ago

avik-pal commented 2 months ago

Currently the functions are written as:

function foo(args...; kwargs...)
    error("ExtX.jl not loaded")
end

# In ExtX.jl
function foo(a::Int, b::Char)
    # do the correct thing
end

Now this works, but let's say I did using X, DynamicExpressions; foo(1, 2) I will get an error "ExtX.jl not loaded" which is somewhat confusing because X.jl is already loaded. For quite a few of my packages the way I handle this is:

@inline _is_extension_loaded(::Val) = false

function _foo_internal end

function foo(...)
    _is_extension_loaded(Val(:X)) && return _foo_internal(...)
    error("....")
end

# In ExtX.jl
@inline _is_extension_loaded(::Val{:X}) = true

This does cause a minor increase in invalidations, but julia compiles away the branch so there is no runtime cost

MilesCranmer commented 1 month ago

Good point, thanks!