diff --git a/README.md b/README.md index 0d6e312..e2549a6 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,10 @@ This package aims to provide light-weight performant tooling without requiring a Alternatively see the [MacroTools](https://github.com/MikeInnes/MacroTools.jl) package for more powerful set of tools. -Currently, this package provides the `splitdef` and `combinedef` functions which are useful for inspecting and manipulating function definition expressions. +Currently, this package provides the `splitdef`, `signature` and `combinedef` functions which are useful for inspecting and manipulating function definition expressions. + - `splitdef` works on a function definition expression and returns a `Dict` of its parts. + - `signature` works on a `Method` returning a similar `Dict` that holds the parts of the expressions that would form its signature. + - `combinedef` takes such `Dict` and builds it back into an expression e.g. ```julia @@ -40,6 +43,18 @@ julia> def[:head] = :(=); julia> def[:body] = :(x * y); -julia> combinedef(def) +julia> g_expr = combinedef(def) :((g(x::T, y::T) where T) = x * y) -``` \ No newline at end of file + +julia> eval(g_expr) +g (generic function with 1 method) + +julia> g_method = first(methods(g)) +g(x::T, y::T) where T in Main + +julia> signature(g_method) +Dict{Symbol,Any} with 3 entries: + :name => :g + :args => Expr[:(x::T), :(y::T)] + :whereparams => Any[:T] +``` diff --git a/docs/src/api.md b/docs/src/api.md index 9d110ad..f005fd7 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -10,4 +10,5 @@ CurrentModule = ExprTools ```@docs splitdef combinedef +signature ``` diff --git a/docs/src/index.md b/docs/src/index.md index d0bbcb6..08ec38b 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -5,7 +5,16 @@ This package aims to provide light-weight performant tooling without requiring a Alternatively see the [MacroTools](https://github.com/MikeInnes/MacroTools.jl) package for more powerful set of tools. -Currently, this package provides the `splitdef` and `combinedef` functions which are useful for inspecting and manipulating function definition expressions. + +ExprTools provides tooling for working with Julia expressions during [metaprogramming](https://docs.julialang.org/en/v1/manual/metaprogramming/). +This package aims to provide light-weight performant tooling without requiring additional package dependencies. + +Alternatively see the [MacroTools](https://github.com/MikeInnes/MacroTools.jl) package for more powerful set of tools. + +Currently, this package provides the `splitdef`, `signature` and `combinedef` functions which are useful for inspecting and manipulating function definition expressions. + - [`splitdef`](@ref) works on a function definition expression and returns a `Dict` of its parts. + - [`signature`](@ref) works on a `Method` returning a similar `Dict` that holds the parts of the expressions that would form its signature. + - [`combinedef`](@ref) takes such `Dict` and builds it back into an expression e.g. ```jldoctest @@ -35,6 +44,18 @@ julia> def[:head] = :(=); julia> def[:body] = :(x * y); -julia> combinedef(def) +julia> g_expr = combinedef(def) :((g(x::T, y::T) where T) = x * y) -``` \ No newline at end of file + +julia> eval(g_expr) +g (generic function with 1 method) + +julia> g_method = first(methods(g)) +g(x::T, y::T) where T in Main + +julia> signature(g_method) +Dict{Symbol,Any} with 3 entries: + :name => :g + :args => Expr[:(x::T), :(y::T)] + :whereparams => Any[:T] +``` diff --git a/src/type_utils.jl b/src/type_utils.jl index a0bbc91..110cb5d 100644 --- a/src/type_utils.jl +++ b/src/type_utils.jl @@ -3,10 +3,7 @@ Extracts the type-parameters of the `type`. -```jldoctest -julia> parameters(Foo{A, B, C}) == [A, B, C] -true -``` +e.g. `parameters(Foo{A, B, C}) == [A, B, C]` """ parameters(sig::UnionAll) = parameters(sig.body) parameters(sig::DataType) = sig.parameters