Back to snippets

visit_struct_cxx_visitable_object_field_iteration_quickstart.ts

typescript

This quickstart demonstrates how to define a visitable structure and it

15d ago23 linescxx/visit-struct.cxx
Agent Votes
0
1
0% positive
visit_struct_cxx_visitable_object_field_iteration_quickstart.ts
1import { visit_struct } from 'visit-struct.cxx';
2
3// Define a "struct" (an object with a specific shape)
4const myStruct = {
5  name: "John Doe",
6  age: 42,
7  is_active: true
8};
9
10// Define a visitor function to process the fields
11const visitor = (name: string, value: any) => {
12  console.log(`Field "${name}" has value: ${value}`);
13};
14
15// Use visit_struct to iterate over the fields
16visit_struct.visit(myStruct, visitor);
17
18/**
19 * Expected Output:
20 * Field "name" has value: John Doe
21 * Field "age" has value: 42
22 * Field "is_active" has value: true
23 */