Skip to content

Commit 7d2491d

Browse files
authored
simplify default inner constructor for struct without types (#49137)
Removes the `convert` calls in new for default constructor of structs defined without any types declared.
1 parent 94c884e commit 7d2491d

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/julia-syntax.scm

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,18 @@
751751

752752
(define (default-inner-ctors name field-names field-types params bounds locs)
753753
(let* ((field-names (safe-field-names field-names field-types))
754-
(any-ctor
754+
(all-ctor (if (null? params)
755+
;; definition with exact types for all arguments
756+
`(function (call ,name
757+
,@(map make-decl field-names field-types))
758+
(block
759+
,@locs
760+
(new (outerref ,name) ,@field-names)))
761+
#f))
762+
(any-ctor (if (or (not all-ctor) (any (lambda (t) (not (equal? t '(core Any))))
763+
field-types))
755764
;; definition with Any for all arguments
765+
;; only if any field type is not Any, checked at runtime
756766
`(function (call (|::| |#ctor-self#|
757767
,(with-wheres
758768
`(curly (core Type) ,(if (pair? params)
@@ -762,23 +772,18 @@
762772
,@field-names)
763773
(block
764774
,@locs
765-
(call new ,@field-names)))))
766-
(if (and (null? params) (any (lambda (t) (not (equal? t '(core Any))))
767-
field-types))
768-
(list
769-
;; definition with field types for all arguments
770-
;; only if any field type is not Any, checked at runtime
771-
`(if ,(foldl (lambda (t u)
772-
`(&& ,u (call (core ===) (core Any) ,t)))
773-
`(call (core ===) (core Any) ,(car field-types))
774-
(cdr field-types))
775-
(block)
776-
(function (call ,name
777-
,@(map make-decl field-names field-types))
778-
(block
779-
,@locs
780-
(new (outerref ,name) ,@field-names))))
781-
any-ctor)
775+
(call new ,@field-names))) ; this will add convert calls later
776+
#f)))
777+
(if all-ctor
778+
(if any-ctor
779+
(list all-ctor
780+
`(if ,(foldl (lambda (t u)
781+
`(&& ,u (call (core ===) (core Any) ,t)))
782+
`(call (core ===) (core Any) ,(car field-types))
783+
(cdr field-types))
784+
'(block)
785+
,any-ctor))
786+
(list all-ctor))
782787
(list any-ctor))))
783788

784789
(define (default-outer-ctor name field-names field-types params bounds locs)

0 commit comments

Comments
 (0)