-
While running my Toit code, I have an object and I want to get the class name of it as a string. Something like this:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Toit programs use class ids (small integers) instead of class names at runtime. This is to save space on memory constrained devices like the ESP32, where Toit programs are stripped of their unnecessary debug information. The information can easily be preserved in a snapshot file, where it can be queried through the Getting the class idSometimes, it is useful to get the class name of a specific object or to translate class ids to class names.
you will see something along the lines of
which prints Translating class ids to class namesWe can use a snapshot that contains the debug information to translate the class id to the corresponding class name. First we need to put the above code in a file. Let's call it
Then we can compile the program into a snapshot using the Toit compiler and run it from the snapshot: build/host/bin/toitc -w example.snapshot example.toit
build/host/bin/toitvm example.snapshot This generates the The build/host/bin/toitvm tools/toitp.toit -c example.snapshot Alternatively, we can also write a small piece of Toit code that uses
It prints |
Beta Was this translation helpful? Give feedback.
Toit programs use class ids (small integers) instead of class names at runtime. This is to save space on memory constrained devices like the ESP32, where Toit programs are stripped of their unnecessary debug information. The information can easily be preserved in a snapshot file, where it can be queried through the
tools/snapshot.toit
utility code.Getting the class id
Sometimes, it is useful to get the class name of a specific object or to translate class ids to class names.
If you write this code and run it:
you will see something along the lines of
an instance with class-id 31
printed. Another w…