Compound data type: Difference between revisions

Content added Content deleted
(Switched the Common Lisp and D entries to the correct order)
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
POINT</lang>
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
#S(POINT :X 0 :Y 0)
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
5.5
CL-USER> (point-y b)
#C(0 1)
CL-USER> (setf (point-y b) 3) ;The accessor is setfable
3
CL-USER> (point-y b)
3</lang>


=={{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
POINT</lang>
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
#S(POINT :X 0 :Y 0)
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
5.5
CL-USER> (point-y b)
#C(0 1)
CL-USER> (setf (point-y b) 3) ;The accessor is setfable
3
CL-USER> (point-y b)
3</lang>


=={{header|E}}==
=={{header|E}}==