Repository navigation
Visitfields
Home › Miscellaneous Keywords › VisitFields
Calls a visitor for each field of a derive fields type, with the field's name and value.
visitfields( x, v )-
x: the value whose fields are visited. Its type must have aderive fieldsline (seeDerive). -
v: the visitor, a value of a type of your own withVisitmembers and, where the fields need them,Enter,LeaveandVisitList.
visitfields is a statement. The compiler writes one ordinary call on v for each field of x's type, so a serialiser, a decoder or a debug dump is written once, as a visitor, and works for every type that opts in with derive fields. Nothing happens at run time that the calls do not do: there is no type information kept in the program.
The fields are visited in order: the base type first (if any), as a field named base, then each field in declaration order. Private fields are visited too, because the type's author asked for it with derive fields. Static members are not fields of a value, and are skipped.
For each field, the kind of the field's value decides the call:
| The field is | The call |
|---|---|
a type with derive fields
|
v.Enter( "name" ), then the same rule over that type's fields, then v.Leave( )
|
a containers.Array( of U )
|
v.VisitList( "name", x.field ) |
anything else: a number, a string, an enum, a pointer, a fixed or dynamic array, a type without derive fields
|
v.Visit( "name", x.field ) |
The name is a string literal holding the field's name as it is spelled in the declaration, in its own case. Visit's first parameter can be a ustring, a string or a zstring ptr.
Each call is an ordinary call, checked as if you had written it out:
-
Overloads choose. One
Visitoverload per field type is the usual visitor. Abyrefparameter takes no widening, so anint16field does not reachbyref x as const int32: give each field type its own overload, or take the valuebyval. -
Strings cross over as in FreeBASIC. With both a
stringand azstringoverload, astringfield reachesbyref as const zstringand azstring * Nfield reachesbyref as const string. -
An array field reaches a parameter declared
a( ) as const T. -
Constness follows
x. Ifxisbyref as const, every field is const, and an encoder's overloads takebyref as const. Ifxis a variable, a decoder's overloads takebyrefand assign to the field.
A visitor that meets containers.Array fields writes one generic, VisitList( of U ), whose body calls visitvalue for each element; visitvalue applies the same rule to the element, so the compiler picks Visit, Enter / Leave or another VisitList for each U. Keep the generic under its own name: tiko, like FreeBASIC, allows one generic procedure per name and does not rank a generic against ordinary overloads of the same name.
x and v must each name a place: a variable, a field, an array element, a dereference or this. Every call the compiler writes reads x again, so a procedure's result or another expression is refused (TKO1282).
In a generic procedure, visitfields( x, v ) over a value of a type parameter is checked for each instance.
#import "std/containers"
type Entity
derive fields
as int64 id
end type
type Point
derive fields
as float64 x, y
end type
type Shape extends Entity
derive fields
as ustring label
as Point origin
as containers.Array( of Point ) corners
static as int32 made '' static: not walked
private:
as int32 version = 3 '' private: walked, because the type opted in
end type
dim as int32 Shape.made
'' Prints each field on its own line, indented by depth.
type Dump
as integer depth
declare sub Visit( byval nm as ustring, byref x as const int32 )
declare sub Visit( byval nm as ustring, byref x as const int64 )
declare sub Visit( byval nm as ustring, byref x as const float64 )
declare sub Visit( byval nm as ustring, byref x as const ustring )
declare sub Enter( byval nm as ustring )
declare sub Leave( )
declare sub VisitList( of U )( byval nm as ustring, byref a as containers.Array( of U ) )
end type
sub Dump.Visit( byval nm as ustring, byref x as const int32 )
print space( depth * 2 ); nm; " ="; x
end sub
sub Dump.Visit( byval nm as ustring, byref x as const int64 )
print space( depth * 2 ); nm; " ="; x
end sub
sub Dump.Visit( byval nm as ustring, byref x as const float64 )
print space( depth * 2 ); nm; " ="; x
end sub
sub Dump.Visit( byval nm as ustring, byref x as const ustring )
print space( depth * 2 ); nm; " = """; x; """"
end sub
sub Dump.Enter( byval nm as ustring )
print space( depth * 2 ); nm; " {"
depth += 1
end sub
sub Dump.Leave( )
depth -= 1
print space( depth * 2 ); "}"
end sub
sub Dump.VisitList( of U )( byval nm as ustring, byref a as containers.Array( of U ) )
print space( depth * 2 ); nm; " ["; a.Count( ); " ]"
depth += 1
for i as integer = 0 to a.Count( ) - 1
visitvalue( "#" & i, a[ i ], this )
next
depth -= 1
end sub
dim as Shape s
s.id = 41
s.label = "tri"
s.origin = type<Point>( 1.5, 2 )
s.corners.Push( type<Point>( 0, 0 ) )
s.corners.Push( type<Point>( 4, 0 ) )
dim as Dump d
visitfields( s, d )base {
id = 41
}
label = "tri"
origin {
x = 1.5
y = 2
}
corners [ 2 ]
#0 {
x = 0
y = 0
}
#1 {
x = 4
y = 0
}
version = 3
| Mistake | Diagnostic |
|---|---|
x's type has no derive fields, or is not a type at all |
TKO1280 |
visitfields where a value is wanted, as in r = visitfields( s, p ): it has no result |
TKO1281 |
x or v is an expression, not a place |
TKO1282 |
| not exactly two arguments | TKO1283 |
no Visit overload takes a field |
the ordinary overload error, TKO0698 or TKO0632, at the visitfields line |
a nested derive fields field, and v has no Enter or Leave
|
the ordinary missing-member error, TKO0739, at the visitfields line |
type Location
as int32 aisle, bin
end type
type Printer
as integer lines
declare sub Visit( byval nm as ustring, byref x as const int32 )
end type
sub Printer.Visit( byval nm as ustring, byref x as const int32 )
print nm; " ="; x
end sub
dim as Location l
dim as Printer p
visitfields( l, p )shop.tiko(15,1): error TKO1280: visitfields( ) walks a type with 'derive fields', and 'location' has none
|
15 | visitfields( l, p )
| ^
derive fields itself refuses a bitfield field (TKO0992: a bitfield has no address), a union cannot derive at all (TKO1259), and neither can a type with an anonymous union (TKO1260).
-
Not a reserved word. A procedure, variable or other symbol of your own named
visitfieldswins, and the built-in is then out of reach in that scope. - The statement form without parentheses,
visitfields x, v, works too. - The calls are written into the unit that holds the
visitfields, so it works on a type imported from a module, private fields included, and changes no module's interface. -
visitfieldsis a tiko addition.
Category: Miscellaneous Keywords Kind: statement
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Android (experimental)
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Move Semantics
- Match
- Lambdas
- Block Strings
- String Interpolation
- Partial Types
- String Slices
- Array Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Hardware
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- tiko test
- tiko doc
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing