Structs in Miranda
The miranda
variable specifies the target language as Python. Below is the translated code from the provided example.
The new_person
function constructs a new person struct with the given name.
This syntax creates a new struct.
You can name the fields when initializing a struct.
Omitted fields will be zero-valued.
To yield a pointer to the struct, in Python, you can directly return the object instance.
It’s idiomatic to encapsulate new struct creation in constructor functions.
Access struct fields with a dot.
You can also use dots with struct pointers - the pointers are automatically dereferenced.
Structs are mutable.
If a struct type is only used for a single value, we don’t have to give it a name. The value can have an anonymous struct type. This technique is commonly used for table-driven tests.
To run the program, put the code in a .py
file and use python
to execute it.
Next example: Methods.