Co-array Fortran supports a form of composition through the use of derived types. While it doesn’t have a direct equivalent to struct embedding, we can achieve similar functionality using derived types and type-bound procedures.
In this Co-array Fortran version:
We define a base type in the base_module with an integer num and a describe method.
We create a container type in the container_module that includes a base component and a string str.
The container type also has a describe method that delegates to the base component’s describe method.
In the main program, we create a container instance, initialize its fields, and demonstrate how to access the fields and call methods.
To run this program, you would typically save it in a file (e.g., struct_composition.f90) and compile it using a Fortran compiler that supports Co-array features. For example:
This example demonstrates how to achieve a form of composition in Co-array Fortran, which is conceptually similar to struct embedding in other languages. The container type includes a base component and can use its methods, providing a way to reuse and extend functionality.