Created
September 2, 2026 20:45
-
-
Save raspberrypisig/0a146bbc9940a436158f32534f60d992 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- Inspecting User struct --- | |
| Inspecting Type: cell.User | |
| basename User | |
| -> Is a struct | |
| Field: name | |
| Type: String | |
| Field: age | |
| Type: SIMD[DType.int, 1] | |
| --- Inspecting Tuple[String, Int, Bool] --- | |
| Inspecting Tuple | |
| Number of elements: 3 | |
| base name : Tuple | |
| Element type: String | |
| Element type: SIMD[DType.int, 1] | |
| Element type: Bool | |
| --- Inspecting String type --- | |
| Inspecting Type: String | |
| basename String | |
| -> Is a struct | |
| Field: _ptr_or_data | |
| Type: std.memory.pointer.Pointer[True, {}, SIMD[DType.uint8, 1], {}, 0] | |
| Field: _len_or_data | |
| Type: SIMD[DType.int, 1] | |
| Field: _capacity_or_data | |
| Type: SIMD[DType.int, 1] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %%mojo | |
| from std.reflection import reflect | |
| struct User: | |
| var name: String | |
| var age: Int | |
| def inspect[T: AnyType](): | |
| comptime r = reflect[T] | |
| print("Inspecting Type:", r.name()) | |
| print("basename", r.base_name()) | |
| comptime if r.base_name() == "Tuple": | |
| print("-> Is a tuple") | |
| comptime if r.is_struct(): | |
| print("-> Is a struct") | |
| comptime names = r.field_names() | |
| comptime types = r.field_types() | |
| comptime for i in range(r.field_count()): | |
| comptime field_name = names[i] | |
| comptime field_type = types[i] | |
| print(" Field:", field_name) | |
| print(" Type: ", reflect[field_type].name()) | |
| else: | |
| print("-> Is neither a struct nor a tuple.") | |
| # Mojo binds *Ts from the Tuple type. | |
| def inspect_tuple[*Ts: Movable](t: Tuple[*Ts]): | |
| print("Inspecting Tuple") | |
| comptime r = reflect[Tuple[*Ts]] | |
| print(" Number of elements:", len(Ts)) | |
| print(" base name :", r.base_name()) | |
| comptime for i in range(len(Ts)): | |
| comptime element_type = Ts[i] | |
| print(" Element type:", reflect[element_type].name()) | |
| def main(): | |
| print("\n--- Inspecting User struct ---") | |
| inspect[User]() | |
| print("\n--- Inspecting Tuple[String, Int, Bool] ---") | |
| var t = Tuple[String, Int, Bool]( | |
| "hello", | |
| 42, | |
| True | |
| ) | |
| inspect_tuple(t) | |
| print("\n--- Inspecting String type ---") | |
| inspect[String]() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment