Skip to content

Commit 74e392e

Browse files
authored
Added at-consts macro (#133)
Fixes #111 Also removed references to Julia 0.6 and 0.7 in the Readme & docs.
1 parent 4b917d2 commit 74e392e

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 2021-01-22
2+
Added `@consts` macro to define a block of constants.
3+
14
# 2019-09-10
25
Added support of packing immutables (by creating one) through `@pack_SomeType`.
36

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ julia> MyNT(x = 2)
6161
(x = 2, y = "foo", z = :bar)
6262
```
6363
64-
> v0.6 users: since `NamedTuples` are not supported in base Julia v0.6, you must import the `NamedTuples.jl` package. Be aware of [this issue](https://github.com/JuliaLang/julia/issues/17240) with keyword arguments in v0.6.
65-
6664
Unpacking is done with `@unpack` (`@pack!` is similar):
6765
```julia
6866
struct B
@@ -77,6 +75,16 @@ a = BB.a
7775
c = BB.c
7876
```
7977
78+
Defining several constants
79+
```julia
80+
@consts begin
81+
a = 1
82+
b = 2.0
83+
c = "a"
84+
end
85+
```
86+
87+
8088
The features are:
8189
8290
- a keyword constructor for the type
@@ -88,7 +96,8 @@ The features are:
8896
- packing and unpacking macros for the type: `@unpack_*` where `*` is
8997
the type name.
9098
- generic packing and unpacking macros `@pack!`, `@unpack` (work with
91-
any types).
99+
any types, via [UnPack.jl](https://github.com/mauro3/UnPack.jl))
100+
- `@consts` macro to defined a bunch of constants
92101
93102
The keyword-constructor and default-values functionality will probably
94103
make it into Julia

docs/src/manual.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ re-definition warnings, then use [`@with_kw_noshow`](@ref).
136136

137137
As mentioned in the README, the `@with_kw` macro can be used to decorate a named tuple and produce a named tuple constructor with those defaults.
138138

139-
> Users of Julia v0.6 should be aware of this [caveat](https://github.com/JuliaLang/julia/issues/17240) that prohibits assignments like `@with_kw (f = f, x = x)`. This is a consequence of different scoping rules for keyword arguments in [v0.6](https://docs.julialang.org/en/stable/manual/functions/#Evaluation-Scope-of-Default-Values-1) and [v0.7](https://docs.julialang.org/en/latest/manual/functions/#Evaluation-Scope-of-Default-Values-1).
140-
141-
> Users of v0.6 will also need to explicitly import `NamedTuples.jl`, since this functionality is not present in that version of base Julia.
142-
143139
These named tuples can be defined as such:
144140

145141
```julia
@@ -178,6 +174,19 @@ julia> z
178174
179175
Since the macro operates on a single tuple expression (as opposed to a tuple of assignment expressions),writing `@with_kw(x = 1, y = :foo)` will return an error suggesting you write `@with_kw (x = 1, y = :foo)`.
180176
177+
# Blocks of constants
178+
179+
Several constants can be defined like so:
180+
```julia
181+
@consts begin
182+
a = 1
183+
b = 2
184+
c = 3
185+
end
186+
```
187+
(if you do the math, you'll need more than three constants in the block to actually save typing.)
188+
189+
181190
# (Un)pack macros
182191
183192
## `@unpack` and `@pack` re-exported from UnPack.jl

src/Parameters.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import Base: @__doc__
4545
import OrderedCollections: OrderedDict
4646
using UnPack: @unpack, @pack!
4747

48-
export @with_kw, @with_kw_noshow, type2dict, reconstruct, @unpack, @pack!, @pack
48+
export @with_kw, @with_kw_noshow, type2dict, reconstruct, @unpack, @pack!, @pack, @consts
4949

5050
## Parser helpers
5151
#################
@@ -657,4 +657,25 @@ macro with_kw_noshow(typedef)
657657
return esc(with_kw(typedef, __module__, false))
658658
end
659659

660+
###########
661+
# @consts macro
662+
663+
"""
664+
665+
"""
666+
macro consts(block)
667+
@assert block.head == :block
668+
args = block.args
669+
for i in eachindex(args)
670+
a = args[i]
671+
if a isa LineNumberNode
672+
continue
673+
elseif a.head == :(=)
674+
args[i] = Expr(:const, args[i])
675+
else
676+
error("Could not parse block")
677+
end
678+
end
679+
return esc(block)
680+
end
660681
end # module

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,17 @@ z2 = TestModule.test_function(TestModule.TestStruct(; y = 9.0))
668668
@test foo()() == (x = 1, y = 2)
669669
@test foo()(y=4) == (x = 1, y = 4)
670670
end
671+
672+
####
673+
# @consts
674+
####
675+
@consts begin
676+
a34 = 1
677+
b34 = 2.0
678+
d34 = sin(56)
679+
end
680+
@test a34 == 1
681+
@test b34 == 2.0
682+
@test_throws ErrorException global b34 = 1
683+
## this test is dependent on Julia version, leave it:
684+
# @test_warn "WARNING: redefinition of constant b34. This may fail, cause incorrect answers, or produce other errors." global b34 = 4.0

0 commit comments

Comments
 (0)