ADT Values should support named fields #1671
macrologist
started this conversation in
Ideas and Proposals
Replies: 1 comment 1 reply
-
|
I can relate to the motivation for adding named fields. However, regarding the In Common Lisp it only appears in While we could certainly add a new meaning to Here are alternatives that might feel more natural to me: ;; Keyword prefix:
(define-type Shape
(Circle (:radius F32) (:center Point))
(Triangle Point Point Point))
(match shape
((Circle :center center) center)
((Square :center center) center))
...)
;; Dot prefix:
(define-type Shape
(Circle (.radius F32) (.center Point))
(Square (.side F32) (.center Point))
(Triangle Point Point Point))
(match shape
((Circle .center) center)
((Square .center) center))
...)What do you think? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
(define-struct Point (x F32) (y F32)) (define-type (Shape F32) (Circle (@radius F32) (@center Point)) (Square (@side F32) (@center Point)) (Triangle Point Point Point))These named fields would be available for pattern matching
(define (centroid shape) (match shape ((Circle @center) center) ((Square @center) center) ((Triangle a b c) (let (Pt x y) = (sum-points (make-list a b c))) (Pt (/ x 3) (/ y 3))))For the above
Shapetype, this extension does not seem very useful. But in real applications ADT fields may number greater than the 7 plus-or-minus 2 mark, at which point remembering orderings becomes tedious for we pea brained humans.This is an attempt to start a discussion
Beta Was this translation helpful? Give feedback.
All reactions