Skip to content

Visitvalue

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

Home › Miscellaneous Keywords › VisitValue

VisitValue

Calls a visitor for one value under a name, by the rule visitfields uses for a field.

Syntax

visitvalue( name, e, v )

Parameters

  • name: the name passed to the visitor: a string literal or any string expression.
  • e: the value to visit: a variable, a field, an array element, a dereference or this.
  • v: the visitor.

Description

visitvalue is a statement. The kind of e's value decides the call, exactly as for a field of visitfields:

e is The call
a type with derive fields v.Enter( name ), then each of its fields by the same rule, then v.Leave( )
a containers.Array( of U ) v.VisitList( name, e )
anything else v.Visit( name, e )

Each call is an ordinary call: overloads, conversions and constness work as written. See visitfields for the details.

Its main use is inside a visitor's VisitList. A visitor that meets containers.Array fields writes one generic for them:

declare sub VisitList( of U )( byval nm as ustring, byref a as containers.Array( of U ) )

A generic body cannot tell what U is, so it cannot choose between Visit and Enter itself. Its loop calls visitvalue( "", a[ i ], this ), or passes a name of its own such as the index, and the compiler makes the choice for each instance: an Array( of int32 ) element goes to Visit, an element with derive fields goes through Enter and Leave, and an Array( of Array( of int32 ) ) element goes to VisitList again.

Keep the generic under its own name, apart from the Visit overloads: tiko, like FreeBASIC, allows one generic procedure per name and does not rank a generic against ordinary overloads of the same name.

visitvalue also works on its own, outside a visitor, on any value that is a place.

Examples

#import "std/containers"

type Point
    derive fields
    as float64 x, y
end type

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 float64 )
    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 float64 )
    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
'' The one generic: whatever U is, visitvalue picks Visit, Enter/Leave or VisitList for it.
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( nm & "[" & i & "]", a[ i ], this )
    next
    depth -= 1
end sub

dim as int32 total = 7
dim as Point p = ( 3, 4 )
dim as containers.Array( of int32 ) row0, row1
row0.Push( 1 ) : row0.Push( 2 ) : row1.Push( 3 )
dim as containers.Array( of containers.Array( of int32 ) ) grid
grid.Push( row0 ) : grid.Push( row1 )

dim as Dump d
visitvalue( "total", total, d )       '' an int32: Visit
visitvalue( "p", p, d )               '' a derive fields type: Enter, its fields, Leave
visitvalue( "grid", grid, d )         '' an Array: VisitList, whose elements are Arrays again
total = 7
p {
  x = 3
  y = 4
}
grid [ 2 ]
  grid[0] [ 2 ]
    grid[0][0] = 1
    grid[0][1] = 2
  grid[1] [ 1 ]
    grid[1][0] = 3

Errors

Mistake Diagnostic
e or v is an expression, not a place: visitvalue( "n", n + 1, d ) TKO1282
not exactly three arguments TKO1283
no Visit overload takes the value the ordinary overload error, TKO0698 or TKO0632
a derive fields value, and v has no Enter or Leave the ordinary missing-member error, TKO0739

Remarks

  • Not a reserved word. A procedure, variable or other symbol of your own named visitvalue wins, and the built-in is then out of reach in that scope.
  • Unlike visitfields, visitvalue takes a value of any type: one without derive fields simply goes to Visit.
  • visitvalue is a tiko addition.

See also


Category: Miscellaneous Keywords Kind: statement

Clone this wiki locally