I was wondering if one could do the following task without writing the subroutine for every possible input type and then defining and interface.
I want to write a subroutine which takes a message of type character and one variable of a type like: integer, double, float, complex, logical, character, own type(matrix). The subroutine then should just simply print the message and after that print the variable. However I want to use an own print subroutine for my own types. In Java I could override the .toString() - method which is inherited from the general Object class to every other class. Is there a similar way to affect the way how my own type is printed when calling something like print*, VariableOfOwnType
?
And how do I declare such a general argument in Fortran? In Java I could simply say that the argument is of type object. Please share your wisdom :)
Asked
Active
Viewed 124 times
0

v.tralala
- 1,444
- 3
- 18
- 39
-
For your own types you can use user-defined input/output. That's a very broad topic though (and you can find examples in answers to other questions). It may be better if you look at that topic and then ask a specific question that comes up if/when you struggle. [Defined IO doesn't help for generic intrinsic types, that said.] – francescalus Nov 20 '16 at 16:54
-
One example is [here](http://stackoverflow.com/a/22693949), but I won't claim that's a general answer. – francescalus Nov 20 '16 at 16:59
-
Almost always you want to know where the output is coming from (i.e. line number). You could use an f90 module/interface/procedure and have the various types. You will likely need to check the value and or use a syntax like 1PE12.5 for floats. I just have a bunch of FORMAT and WRITE statements and usually a D at the beginning when I no longer want them, and call the code either .for, or use -fixed -132 as compile arguments. – Holmz Nov 21 '16 at 09:45