Custom Types
The built-in types describe single values. Most data comes in groups and in
categories, and tiko has a declaration for each: a type (a record) groups
named fields, an enum names a set of related values, and a const names a
value that never changes.
'' Three kinds of custom type, working together.
'' A constant: a name for a fixed value.
const ReorderLevel = 5
'' An enum: a closed set of named values.
enum Category
Tools
Parts
Supplies
end enum
'' A record: named fields grouped into one value.
type StockItem
as ustring label
as Category kind
as int32 onHand
end type
sub Report( byref item as StockItem )
with item
print .label; " (category"; .kind; "):"; .onHand; " on hand";
if .onHand <= ReorderLevel then print " -- reorder";
print
end with
end sub
dim as StockItem hammer = ( "hammer", Tools, 12 )
dim as StockItem bolt = ( "bolt", Parts, 3 )
dim as StockItem tape = ( "tape", Supplies, 5 )
Report( hammer )
Report( bolt )
Report( tape )
Output
hammer (category 0): 12 on hand
bolt (category 1): 3 on hand -- reorder
tape (category 2): 5 on hand -- reorder
StockItem keeps a label, a category and a count together, so Report takes
one argument instead of three. Its kind field has the enum type Category,
whose members Tools, Parts and Supplies print as 0, 1 and 2.
ReorderLevel gives the number 5 a name, so the test reads as what it means.
The pages in this section take each part in turn:
- Records: declaring fields, filling them, nesting one type in another, and copying.
- with: naming a record once and reaching its fields with a dot.
- Enums: named integer constants.
- Unions: several views of the same bytes.
- Constants:
const, with and without a type.
Methods, constructors and operators on your own types are in Objects.
Activities
- Add a fourth item with a count of 0 and check that it is flagged.
- Add a
Consumablesmember toCategoryand use it fortape.