|
|
Line 194: |
Line 194: |
|
(assert (= 1 (:y p)))</lang> |
|
(assert (= 1 (:y p)))</lang> |
|
|
|
|
|
⚫ |
=={{header|Common Lisp}}== |
|
|
|
|
⚫ |
<lang lisp>CL-USER> (defstruct point (x 0) (y 0)) ;If not provided, x or y default to 0 |
|
⚫ |
|
|
⚫ |
In addition to defining the ''point'' data type, the defstruct macro also created constructor and accessor functions: |
|
⚫ |
<lang lisp>CL-USER> (setf a (make-point)) ;The default constructor using the default values for x and y |
|
⚫ |
|
|
⚫ |
CL-USER> (setf b (make-point :x 5.5 :y #C(0 1))) ;Dynamic datatypes are the default |
|
⚫ |
#S(POINT :X 5.5 :Y #C(0 1)) ;y has been set to the imaginary number i (using the Common Lisp complex number data type) |
|
⚫ |
CL-USER> (point-x b) ;The default name for the accessor functions is structname-slotname |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
CL-USER> (setf (point-y b) 3) ;The accessor is setfable |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
|
|
|
|
=={{header|D}}== |
|
=={{header|D}}== |
Line 211: |
Line 228: |
|
|
|
|
|
There are also other ways to initialize them. The D language also supports tuples. |
|
There are also other ways to initialize them. The D language also supports tuples. |
|
|
|
⚫ |
=={{header|Common Lisp}}== |
|
|
|
|
⚫ |
<lang lisp>CL-USER> (defstruct point (x 0) (y 0)) ;If not provided, x or y default to 0 |
|
⚫ |
|
|
⚫ |
In addition to defining the ''point'' data type, the defstruct macro also created constructor and accessor functions: |
|
⚫ |
<lang lisp>CL-USER> (setf a (make-point)) ;The default constructor using the default values for x and y |
|
⚫ |
|
|
⚫ |
CL-USER> (setf b (make-point :x 5.5 :y #C(0 1))) ;Dynamic datatypes are the default |
|
⚫ |
#S(POINT :X 5.5 :Y #C(0 1)) ;y has been set to the imaginary number i (using the Common Lisp complex number data type) |
|
⚫ |
CL-USER> (point-x b) ;The default name for the accessor functions is structname-slotname |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
CL-USER> (setf (point-y b) 3) ;The accessor is setfable |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
|
|
|
|
|
=={{header|E}}== |
|
=={{header|E}}== |