@@ -45,7 +45,7 @@ import Base: @__doc__
45
45
import OrderedCollections: OrderedDict
46
46
using UnPack: @unpack , @pack!
47
47
48
- export @with_kw , @with_kw_noshow , type2dict, reconstruct, @unpack , @pack! , @pack , @consts
48
+ export @with_kw , @kw_only , @ with_kw_noshow , type2dict, reconstruct, @unpack , @pack! , @pack , @consts
49
49
50
50
# # Parser helpers
51
51
# ################
@@ -326,7 +326,7 @@ macro pack_MM(varname)
326
326
end
327
327
```
328
328
"""
329
- function with_kw (typedef, mod:: Module , withshow= true )
329
+ function with_kw (typedef, mod:: Module , withshow= true , allow_default = true )
330
330
if typedef. head== :tuple # named-tuple
331
331
withshow== false && error (" `@with_kw_noshow` not supported for named tuples" )
332
332
return with_kw_nt (typedef, mod)
@@ -486,18 +486,27 @@ function with_kw(typedef, mod::Module, withshow=true)
486
486
push! (args, k)
487
487
push! (kwargs. args, Expr (:kw ,k,w))
488
488
end
489
- if length (typparas)> 0
490
- tps = stripsubtypes (typparas)
491
- innerc = :( $ tn {$(tps...)} ($ kwargs) where {$ (tps... )} = $ tn {$(tps...)} ($ (args... )))
489
+ if allow_default
490
+ if length (typparas)> 0
491
+ tps = stripsubtypes (typparas)
492
+ innerc = :( $ tn {$(tps...)} ($ kwargs) where {$ (tps... )} = $ tn {$(tps...)} ($ (args... )))
493
+ else
494
+ innerc = :($ tn ($ kwargs) = $ tn ($ (args... )) )
495
+ end
492
496
else
493
- innerc = :($ tn ($ kwargs) = $ tn ($ (args... )) )
497
+ if length (typparas)> 0
498
+ tps = stripsubtypes (typparas)
499
+ innerc = :( $ tn {$(tps...)} ($ kwargs) where {$ (tps... )} = new {$(tps...)} ($ (args... )))
500
+ else
501
+ innerc = :($ tn ($ kwargs) = new ($ (args... )) )
502
+ end
494
503
end
495
504
push! (typ. args[3 ]. args, innerc)
496
505
497
506
# Inner positional constructor: only make it if no inner
498
507
# constructors are user-defined. If one or several are defined,
499
508
# assume that one has the standard positional signature.
500
- if length (inner_constructors)== 0
509
+ if length (inner_constructors)== 0 && allow_default
501
510
if length (typparas)> 0
502
511
tps = stripsubtypes (typparas)
503
512
innerc2 = :( $ tn {$(tps...)} ($ (args... )) where {$ (tps... )} = new {$(tps...)} ($ (args... )) )
@@ -679,6 +688,14 @@ macro with_kw(args...)
679
688
""" )
680
689
end
681
690
691
+ """
692
+ As `@with_kw` but does not declare a default constructor when no inner
693
+ constructor is found.
694
+ """
695
+ macro kw_only (typedef)
696
+ return esc (with_kw (typedef, __module__, true , false ))
697
+ end
698
+
682
699
"""
683
700
As `@with_kw` but does not define a `show` method to avoid annoying
684
701
redefinition warnings.
0 commit comments