@@ -30,6 +30,10 @@ function def_getproperties(T, propertynames)
3030 :(StructHelpers. getproperties (o:: $T ) = $ body)
3131end
3232
33+ function def_selfconstructor (T)
34+ :($ T (self:: $T ) = self)
35+ end
36+
3337function def_kwconstructor (T, propertynames)
3438 call = Expr (:call , T, Expr (:parameters , propertynames... ))
3539 ret = :($ call = $ T ($ (propertynames... )))
@@ -40,6 +44,7 @@ const BATTERIES_DEFAULTS = (
4044 eq = true ,
4145 hash = true ,
4246 kwconstructor = false ,
47+ selfconstructor = true ,
4348 kwshow = false ,
4449 getproperties = true ,
4550 constructorof = true ,
@@ -50,6 +55,7 @@ const BATTERIES_DOCSTRINGS = (
5055 eq = " Define `Base.(==)` structurally." ,
5156 hash = " Define `Base.hash` structurally." ,
5257 kwconstructor = " Add a keyword constructor." ,
58+ selfconstructor = " Add a constructor of the for `T(self::T) = self`" ,
5359 kwshow = " Overload `Base.show` such that the names of each field are printed." ,
5460 getproperties = " Overload `ConstructionBase.getproperties`." ,
5561 constructorof = " Overload `ConstructionBase.constructorof`." ,
@@ -160,6 +166,10 @@ macro batteries(T, kw...)
160166 def = def_kwconstructor (T, fieldnames)
161167 push! (ret. args, def)
162168 end
169+ if nt. selfconstructor
170+ def = def_selfconstructor (T)
171+ push! (ret. args, def)
172+ end
163173 return esc (ret)
164174end
165175
@@ -261,11 +271,13 @@ end
261271const ENUM_BATTERIES_DEFAULTS = (
262272 string_conversion= false ,
263273 symbol_conversion= false ,
274+ selfconstructor= BATTERIES_DEFAULTS. selfconstructor,
264275)
265276
266277const ENUM_BATTERIES_DOCSTRINGS = (
267278 string_conversion= " Add `convert(MyEnum, ::String)`, `MyEnum(::String)`, `convert(String, ::MyEnum)` and `String(::MyEnum)`" ,
268279 symbol_conversion= " Add `convert(MyEnum, ::Symbol)`, `MyEnum(::Symbol)`, `convert(Symbol, ::MyEnum)` and `Symbol(::MyEnum)`" ,
280+ selfconstructor= BATTERIES_DOCSTRINGS. selfconstructor,
269281)
270282
271283if (keys (ENUM_BATTERIES_DEFAULTS) != keys (ENUM_BATTERIES_DOCSTRINGS))
@@ -348,6 +360,10 @@ macro enumbatteries(T, kw...)
348360 ex4 = :(Base. Symbol (x:: $T ) = StructHelpers. symbol_from_enum (x))
349361 push! (ret. args, ex1, ex2, ex3, ex4)
350362 end
363+ if nt. selfconstructor
364+ def = def_selfconstructor (T)
365+ push! (ret. args, def)
366+ end
351367 return esc (ret)
352368end
353369
0 commit comments