Methods in Co-array Fortran
This Co-array Fortran code demonstrates the concept of user-defined types and associated procedures, which is similar to methods in object-oriented programming languages.
In Co-array Fortran, we define a module rect_module
that encapsulates the rect
type and its associated procedures area
and perim
. These procedures are similar to methods in Go, but they’re implemented as module procedures rather than being attached directly to the type.
The rect
type is defined with width
and height
components, similar to the struct in the original code.
The area
and perim
functions take a rect
as an argument and perform calculations on it. This is analogous to methods in Go, but the syntax is different.
In the main program, we create a rect
instance, call the functions, and print the results.
Co-array Fortran doesn’t have pointers in the same way as Go, so we don’t demonstrate pointer vs. value semantics. Instead, we simply call the functions twice to mirror the structure of the original example.
To compile and run this program, you would typically use a Co-array Fortran compiler. The exact commands may vary depending on your specific compiler, but it might look something like this:
Note that Co-array Fortran is an extension of Fortran for parallel programming, but this example doesn’t use any of its parallel features. The core concepts of types and procedures shown here are common to standard Fortran as well.