Skip to content

Commit ab9ef1c

Browse files
committed
improve docs
1 parent 7f5aaae commit ab9ef1c

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ end
2020
@batteries S hash=false # don't overload `Base.hash`
2121
@batteries S kwconstructor=true # add a keyword constructor
2222
```
23+
For all supported options and defaults, consult the docstring:
24+
```julia
25+
julia>?@batteries
26+
```
2327

2428
# Alternatives
2529

src/StructHelpers.jl

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,48 @@ function def_kwconstructor(T, propertynames)
3434
end
3535

3636
const BATTERIES_DEFAULTS = (
37-
eq=true, hash=true,
38-
kwconstructor=false, kwshow=false,
39-
getproperties=true, constructorof=true,
37+
eq = true ,
38+
hash = true ,
39+
kwconstructor = false,
40+
kwshow = false,
41+
getproperties = true ,
42+
constructorof = true ,
4043
)
4144

45+
const BATTERIES_DOCSTRINGS = (
46+
eq = "Define `Base.(==)` structurally.",
47+
hash = "Define `Base.hash` structurally.",
48+
kwconstructor = "Add a keyword constructor.",
49+
kwshow = "Overload `Base.show` such that the names of each field are printed.",
50+
getproperties = "Overload `ConstructionBase.getproperties`.",
51+
constructorof = "Overload `ConstructionBase.constructorof`.",
52+
)
53+
54+
if (keys(BATTERIES_DEFAULTS) != keys(BATTERIES_DOCSTRINGS))
55+
error("""
56+
keys(BATTERIES_DEFAULTS) == key(BATTERIES_DOCSTRINGS) must hold.
57+
Got:
58+
keys(BATTERIES_DEFAULTS) = $(keys(BATTERIES_DEFAULTS))
59+
keys(BATTERIES_DOCSTRINGS) = $(keys(BATTERIES_DOCSTRINGS))
60+
""")
61+
end
62+
@assert keys(BATTERIES_DEFAULTS) == keys(BATTERIES_DOCSTRINGS)
63+
64+
function doc_batteries_options()
65+
lines = map(propertynames(BATTERIES_DEFAULTS)) do key
66+
"* **$key** = $(BATTERIES_DEFAULTS[key]):\n $(BATTERIES_DOCSTRINGS[key])"
67+
end
68+
join(lines, "\n")
69+
end
70+
71+
4272
const ALLOWED_KW = keys(BATTERIES_DEFAULTS)
4373

4474
"""
4575
4676
@batteries T [options]
4777
4878
Automatically derive several methods for type `T`.
49-
Supported options are:
50-
$BATTERIES_DEFAULTS
5179
5280
# Example
5381
```julia
@@ -60,6 +88,10 @@ end
6088
@batteries S hash=false # don't overload `Base.hash`
6189
@batteries S kwconstructor=true # add a keyword constructor
6290
```
91+
92+
Supported options and defaults are:
93+
94+
$(doc_batteries_options())
6395
"""
6496
macro batteries(T, kw...)
6597
nt = parse_all_macro_kw(kw)

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct Empty2 end
2727
@batteries Empty1
2828
@batteries Empty2
2929

30+
struct SErrors;a;b;c;end
3031

3132
@testset "@batteries" begin
3233
@test SBatteries(1,2) == SBatteries(1,2)
@@ -63,7 +64,6 @@ struct Empty2 end
6364
@test Empty1() != Empty2()
6465
@test hash(Empty1()) != hash(Empty2())
6566

66-
struct SErrors;a;b;c;end
6767
@test_throws Exception @macroexpand @batteries SErrors kwconstructor="true"
6868
@test_throws Exception @macroexpand @batteries SErrors nonsense=true
6969
@macroexpand @batteries SErrors kwconstructor=true

0 commit comments

Comments
 (0)