Skip to content

Visitfields

Paul Squires edited this page Nov 30, 2026 · 1 revision

Home › Miscellaneous Keywords › VisitFields

VisitFields

Calls a visitor for each field of a derive fields type, with the field's name and value.

Syntax

visitfields( x, v )

Parameters

  • x: the value whose fields are visited. Its type must have a derive fields line (see Derive).
  • v: the visitor, a value of a type of your own with Visit members and, where the fields need them, Enter, Leave and VisitList.

Description

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 Visit overload per field type is the usual visitor. A byref parameter takes no widening, so an int16 field does not reach byref x as const int32: give each field type its own overload, or take the value byval.
  • Strings cross over as in FreeBASIC. With both a string and a zstring overload, a string field reaches byref as const zstring and a zstring * N field reaches byref as const string.
  • An array field reaches a parameter declared a( ) as const T.
  • Constness follows x. If x is byref as const, every field is const, and an encoder's overloads take byref as const. If x is a variable, a decoder's overloads take byref and 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.

Examples

#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

Errors

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).

Remarks

  • Not a reserved word. A procedure, variable or other symbol of your own named visitfields wins, 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.
  • visitfields is a tiko addition.

See also


Category: Miscellaneous Keywords Kind: statement

Clone this wiki locally