Compound data type: Difference between revisions

Add CLU
(Add CLU)
Line 520:
(assert (= 0 (:x p)))
(assert (= 1 (:y p)))</lang>
 
=={{header|CLU}}==
CLU has two types of compound datatypes: ''struct''s, which are immutable, and ''record''s, which are mutable.
Aside from this, they work the same way.
 
<lang clu>% Definitions
point = struct[x, y: int]
mutable_point = record[x, y: int]
 
% Initialization
p: point := point${x: 10, y: 20}
mp: mutable_point := mutable_point${x: 10, y: 20}</lang>
 
The fields can be accessed using the <code>.</code> syntax:
<lang clu>foo := p.x
bar := p.y</lang>
 
''Record''s, but not ''struct''s, allow updating the fields in the same way.
<lang clu>mp.x := 30
mp.y := 40</lang>
 
It should be noted that the special forms <code>p.x</code> and <code>mp.x := value</code>
are really only syntactic sugar, they are equivalent to the following method calls:
<lang clu>foo := point$get_x(p)
bar := point$get_y(p)</lang>
 
<lang clu>mutable_point$set_x(mp, 30)
mutable_point$set_y(mp, 40)</lang>
 
=={{header|COBOL}}==
2,094

edits