Skip to content

Commit d302d03

Browse files
authored
Merge pull request #157 from fingolfin/mh/ext-docs
Improve documentation
2 parents dbfa2db + 09d1973 commit d302d03

File tree

2 files changed

+19
-40
lines changed

2 files changed

+19
-40
lines changed

docs/src/utilities.md

+5-30
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,11 @@ julia> MacroTools.shortdef(ex)
3232
More generally it's also possible to use `splitdef` and `combinedef` to handle
3333
the full range of function syntax.
3434

35-
`splitdef(def)` matches a function definition of the form
36-
37-
```julia
38-
function name(args; kwargs)::rtype where {whereparams}
39-
body
40-
end
41-
```
42-
43-
and returns `Dict(:name=>..., :args=>..., etc.)`. The definition can be rebuilt by
44-
calling `MacroTools.combinedef(dict)`, or explicitly with
45-
46-
```julia
47-
rtype = get(dict, :rtype, :Any)
48-
:(function $(dict[:name])($(dict[:args]...);
49-
$(dict[:kwargs]...))::$rtype where {$(dict[:whereparams]...)}
50-
$(dict[:body].args...)
51-
end)
52-
```
53-
54-
`splitarg(arg)` matches function arguments (whether from a definition or a function call)
55-
such as `x::Int=2` and returns `(arg_name, arg_type, slurp, default)`. `default` is
56-
`nothing` when there is none. For example:
57-
58-
```julia
59-
julia> map(splitarg, (:(f(y, a=2, x::Int=nothing, args...))).args[2:end])
60-
4-element Array{Tuple{Symbol,Symbol,Bool,Any},1}:
61-
(:y, :Any, false, nothing)
62-
(:a, :Any, false, 2)
63-
(:x, :Int, false, :nothing)
64-
(:args, :Any, true, nothing)
35+
```@docs
36+
MacroTools.splitdef
37+
MacroTools.combinedef
38+
MacroTools.splitarg
39+
MacroTools.combinearg
6540
```
6641

6742
## Other Utilities

src/utils.jl

+14-10
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,7 @@ end
289289
```
290290
291291
and return `Dict(:name=>..., :args=>..., etc.)`. The definition can be rebuilt by
292-
calling `MacroTools.combinedef(dict)`, or explicitly with
293-
294-
```julia
295-
rtype = get(dict, :rtype, :Any)
296-
all_params = [get(dict, :params, [])..., get(dict, :whereparams, [])...]
297-
:(function \$(dict[:name]){\$(all_params...)}(\$(dict[:args]...);
298-
\$(dict[:kwargs]...))::\$rtype
299-
\$(dict[:body])
300-
end)
301-
```
292+
calling `MacroTools.combinedef(dict)`.
302293
303294
See also: [`combinedef`](@ref)
304295
"""
@@ -344,6 +335,19 @@ end
344335
345336
`combinedef` is the inverse of [`splitdef`](@ref). It takes a `splitdef`-like Dict
346337
and returns a function definition.
338+
339+
This function approximately does the following (but more sophisticated to avoid
340+
emitting parts that did not actually appear in the original function definition.)
341+
342+
```julia
343+
rtype = get(dict, :rtype, :Any)
344+
all_params = [get(dict, :params, [])..., get(dict, :whereparams, [])...]
345+
:(function \$(dict[:name]){\$(all_params...)}(\$(dict[:args]...);
346+
\$(dict[:kwargs]...))::\$rtype
347+
\$(dict[:body])
348+
end)
349+
```
350+
347351
"""
348352
function combinedef(dict::Dict)
349353
rtype = get(dict, :rtype, nothing)

0 commit comments

Comments
 (0)