IsDef.jl
IsDef.jl copied to clipboard
maintainable type inference
IsDef.jl
This package provides primitives for dispatching on whether certain methods are implemented or not.
For installation or usage, open julia repl and run
using IsDef
which gives you access to following functions
-
isdef(f, arg1, arg2)::Bool/isdef(f, Arg1Type, Arg2Type)::Boolchecks whether a function is defined for the given types.If at least one of the arguments is not a type, all arguments are automatically converted to types for you.
-
Out(f, arg1, arg2)::ReturnType/Out(f, Arg1Type, Arg2Type)::ReturnTypereturns the returntype of the given functioncall.Note, that
Outmay return an abstract type that is wider than necessary, like e.g.Any. If a functioncall is not defined, or predictably throws an error,IsDef.NotApplicableis returned.Outis internally used byisdef.
Internally of Out(f, Arg1Type, Arg2Type) a one-argument-version of Out is used which expects a single Tuple type, specifying the entire call signature. This is the heart of the IsDef package. For the example it would be
Out(Tuple{typeof(f), Arg1Type, Arg2Type})
If you want to specify inference of your method (output of Out), or whether it is defined (output of isdef), you need to overload this very one-argument method of Out. For the example it could be
Out(::Type{<:Tuple{typeof(f), Arg1Type, Arg2Type, Vararg}}) = ReturnType
Enjoy maintainable type inference.