Records
A type declares a record: a set of named fields that travel together as
one value. Each field has a name and a type, and that type can itself be a
record.
'' A type groups named fields into one value.
type Point
as int32 x, y
end type
'' Fields can be other types, and can declare a starting value.
type Frame
as Point origin
as Point size = ( 640, 480 )
as ustring title = "untitled"
as boolean visible = true
end type
'' A list in parentheses fills the fields in declaration order.
dim as Point corner = ( 3, 4 )
print "corner:"; corner.x; corner.y
'' Fields you do not set start at their declared value, or zero.
dim as Frame w
print "fresh frame: "; w.title; ","; w.size.x; " x"; w.size.y; _
", origin"; w.origin.x; w.origin.y; ", visible "; w.visible
'' A dot reaches a field; a second dot reaches into a nested type.
w.title = "editor"
w.origin = corner
w.size.x = 800
print "edited: "; w.title; ","; w.size.x; " x"; w.size.y; _
", origin"; w.origin.x; w.origin.y
'' Assignment copies every field. The copy is independent.
dim as Frame twin = w
twin.title = "twin"
twin.origin.x = 99
print "original: "; w.title; w.origin.x
print "twin: "; twin.title; twin.origin.x
Output
corner: 3 4
fresh frame: untitled, 640 x 480, origin 0 0, visible true
edited: editor, 800 x 480, origin 3 4
original: editor 3
twin: twin 99
as int32 x, y declares two fields of one type on one line, the same way
dim declares variables. A field may carry a starting value, as size,
title and visible do; every new Frame starts with those values, and the
fields without one start at zero, or "" for text.
A list in parentheses, ( 3, 4 ), fills the fields of a record in the order
they are declared. A record nested inside another takes its own list, as in
size as Point = ( 640, 480 ).
A dot reaches a field, and a chain of dots reaches into nested records:
w.size.x. A whole record can be assigned at once, as w.origin = corner
does.
Assignment copies every field, text included. Changing twin afterwards
leaves w as it was: two record variables never share their contents.
Records cannot be compared with = until you define what equality means
for them: a = b on two Points is error
TKO0720: no operator = compares this type.
Objects shows how to define it. Field layout and padding
are on the Sizes page.
Activities
- Add a
Recttype holding twoPoints, and print its width and height. - Write a function that takes a
Pointand returns a new one moved by 10 in each direction.