Templates
The docstrings for @template and DOCSTRING are in the reference.
@template wraps every later docstring of a given kind in a module, so the abbreviations are written once. DOCSTRING marks where each original docstring goes.
- A template applies only to docstrings that come after it, so define templates before the definitions that use them.
- A template applies only in the module that defines it. A submodule needs its own, or a copy made with
@template CATEGORY = ParentModule. - Abstract types use the
ABSTRACTTYPEStemplate, or theTYPEStemplate when the module defines noABSTRACTTYPESone.
module Geometry
using DocStringExtensions
"""
This docstring comes before the `@template` definitions below, so it renders as written.
"""
unit_circle() = Circle(1.0)
@template (FUNCTIONS, METHODS) = """
$(TYPEDSIGNATURES)
$(DOCSTRING)
"""
@template TYPES = """
$(TYPEDEF)
$(DOCSTRING)
$(TYPEDFIELDS)
"""
@template ABSTRACTTYPES = """
$(TYPEDEF)
$(DOCSTRING)
$(SUBTYPES)
"""
"""
This docstring holds only this sentence. `Shape` is abstract, so the `ABSTRACTTYPES` template
adds its subtypes below it in place of fields.
"""
abstract type Shape end
"""
This docstring holds only this sentence. The template adds the definition above it and the
fields below it.
"""
struct Circle <: Shape
"The distance from the centre to the edge."
radius::Float64
end
"""
This docstring holds only this sentence. The template adds the signature above it.
"""
area(c::Circle) = π * c.radius^2
module Plain
using DocStringExtensions
import ..Geometry: Circle
"""
`Plain` is a submodule of `Geometry` but defines no template of its own, so this docstring
renders as written.
"""
perimeter(c::Circle) = 2π * c.radius
end
module Copied
using DocStringExtensions
import ..Geometry
import ..Geometry: Circle
@template (FUNCTIONS, METHODS) = Geometry
"""
`Copied` copies the function template from `Geometry`, so the signature is added here too.
"""
diameter(c::Circle) = 2 * c.radius
end
end
The docstrings render as:
Examples.Geometry.unit_circle — Function
This docstring comes before the @template definitions below, so it renders as written.
Examples.Geometry.Shape — Type
abstract type ShapeThis docstring holds only this sentence. Shape is abstract, so the ABSTRACTTYPES template adds its subtypes below it in place of fields.
Examples.Geometry.Circle — Type
struct Circle <: Examples.Geometry.ShapeThis docstring holds only this sentence. The template adds the definition above it and the fields below it.
radius::Float64: The distance from the centre to the edge.
Examples.Geometry.area — Function
area(c::Examples.Geometry.Circle) -> Float64This docstring holds only this sentence. The template adds the signature above it.
Examples.Geometry.Plain.perimeter — Function
Plain is a submodule of Geometry but defines no template of its own, so this docstring renders as written.
Examples.Geometry.Copied.diameter — Function
diameter(c::Examples.Geometry.Circle) -> Float64Copied copies the function template from Geometry, so the signature is added here too.