22# https://github.com/mauro3/Parameters.jl/pull/147
33# It can be removed when equivalent functionality is in Parameters.jl or another
44# package
5+ # + Comments removed to avoid problems with doctest
56__precompile__ ()
67
7- """
8- This is a package I use to handle numerical-model parameters,
9- thus the name. However, it should be useful otherwise too.
10- It has two main features:
11- - keyword type constructors with default values, and
12- - unpacking and packing of composite types and dicts.
13-
14- The macro `@with_kw` which decorates a type definition to
15- allow default values and a keyword constructor:
16-
17- ```
18- julia> using Parameters
19-
20- julia> @with_kw struct A
21- a::Int = 6
22- b::Float64 = -1.1
23- c::UInt8
24- end
25-
26- julia> A(c=4)
27- A
28- a: 6
29- b: -1.1
30- c: 4
31- ```
32-
33- Unpacking is done with `@unpack` (`@pack!` is similar):
34- ```
35- struct B
36- a
37- b
38- c
39- end
40- @unpack a, c = B(4,5,6)
41- # is equivalent to
42- BB = B(4,5,6)
43- a = BB.a
44- c = BB.c
45- ```
46- """
478module Parameters
489import Base: @__doc__
4910import OrderedCollections: OrderedDict
176137
177138# # Exported helper functions
178139# ####################
179- """
180- Transforms a type-instance into a dictionary.
181-
182- ```
183- julia> struct T
184- a
185- b
186- end
187-
188- julia> type2dict(T(4,5))
189- Dict{Symbol,Any} with 2 entries:
190- :a => 4
191- :b => 5
192- ```
193-
194- Note that this uses `getproperty`.
195- """
196140function type2dict (dt)
197141 di = Dict {Symbol,Any} ()
198142 for n in propertynames (dt)
@@ -201,45 +145,6 @@ function type2dict(dt)
201145 di
202146end
203147
204- """
205- reconstruct(pp; kws...
206- reconstruct(T::Type, pp; kws...)
207-
208- Make a new instance of a type with the same values as the input type
209- except for the fields given in the keyword args. Works for types, Dicts,
210- and NamedTuples. Can also reconstruct to another type, which is probably
211- mostly useful for parameterised types where the parameter changes on
212- reconstruction.
213-
214- Note: this is not very performant. Check Setfield.jl for a faster &
215- nicer implementation.
216-
217- ```jldoctest
218- julia> using Parameters
219-
220- julia> struct A
221- a
222- b
223- end
224-
225- julia> x = A(3,4)
226- A(3, 4)
227-
228- julia> reconstruct(x, b=99)
229- A(3, 99)
230-
231- julia> struct B{T}
232- a::T
233- b
234- end
235-
236- julia> y = B(sin, 1)
237- B{typeof(sin)}(sin, 1)
238-
239- julia> reconstruct(B, y, a=cos) # note reconstruct(y, a=cos) errors!
240- B{typeof(cos)}(cos, 1)
241- ```
242- """
243148reconstruct (pp:: T , di) where T = reconstruct (T, pp, di)
244149reconstruct (pp; kws... ) = reconstruct (pp, kws)
245150reconstruct (T:: Type , pp; kws... ) = reconstruct (T, pp, kws)
@@ -293,43 +198,6 @@ function _pack_new(T, fields)
293198 Expr (:call , T, fields... )
294199end
295200
296- """
297- This function is called by the `@with_kw` macro and does the syntax
298- transformation from:
299-
300- ```julia
301- @with_kw struct MM{R}
302- r::R = 1000.
303- a::R
304- end
305- ```
306-
307- into
308-
309- ```julia
310- struct MM{R}
311- r::R
312- a::R
313- MM{R}(r,a) where {R} = new(r,a)
314- MM{R}(;r=1000., a=error("no default for a")) where {R} = MM{R}(r,a) # inner kw, type-paras are required when calling
315- end
316- MM(r::R,a::R) where {R} = MM{R}(r,a) # default outer positional constructor
317- MM(;r=1000,a=error("no default for a")) = MM(r,a) # outer kw, so no type-paras are needed when calling
318- MM(m::MM; kws...) = reconstruct(mm,kws)
319- MM(m::MM, di::Union{AbstractDict, Tuple{Symbol,Any}}) = reconstruct(mm, di)
320- macro unpack_MM(varname)
321- esc(quote
322- r = varname.r
323- a = varname.a
324- end)
325- end
326- macro pack_MM(varname)
327- esc(quote
328- varname = $Parameters .reconstruct(varname,r=r,a=a)
329- end)
330- end
331- ```
332- """
333201function with_kw (typedef, mod:: Module , withshow= true , allow_default= true )
334202 if typedef. head== :tuple # named-tuple
335203 withshow== false && error (" `@with_kw_noshow` not supported for named tuples" )
@@ -634,9 +502,6 @@ function with_kw(typedef, mod::Module, withshow=true, allow_default=true)
634502 end
635503end
636504
637- """
638- Do the with-kw stuff for named tuples.
639- """
640505function with_kw_nt (typedef, mod)
641506 kwargs = []
642507 args = []
@@ -667,20 +532,6 @@ function with_kw_nt(typedef, mod)
667532 end
668533end
669534
670- """
671- Macro which allows default values for field types and a few other features.
672-
673- Basic usage:
674-
675- ```julia
676- @with_kw struct MM{R}
677- r::R = 1000.
678- a::Int = 4
679- end
680- ```
681-
682- For more details see manual.
683- """
684535macro with_kw (typedef)
685536 return esc (with_kw (typedef, __module__, true ))
686537end
@@ -692,37 +543,17 @@ macro with_kw(args...)
692543 """ )
693544end
694545
695- """
696- As `@with_kw` but does not declare a default constructor when no inner
697- constructor is found.
698- """
699546macro kw_only (typedef)
700547 return esc (with_kw (typedef, __module__, true , false ))
701548end
702549
703- """
704- As `@with_kw` but does not define a `show` method to avoid annoying
705- redefinition warnings.
706-
707- ```julia
708- @with_kw_noshow struct MM{R}
709- r::R = 1000.
710- a::Int = 4
711- end
712- ```
713-
714- For more details see manual.
715- """
716550macro with_kw_noshow (typedef)
717551 return esc (with_kw (typedef, __module__, false ))
718552end
719553
720554# ##########
721555# @consts macro
722556
723- """
724-
725- """
726557macro consts (block)
727558 @assert block. head == :block
728559 args = block. args
@@ -738,4 +569,4 @@ macro consts(block)
738569 end
739570 return esc (block)
740571end
741- end # module
572+ end # module
0 commit comments