Description
In #14 @milancurcic indicated he'd like to have something along the lines of a printf
function, like in C. I suggested a way to do this:
C-style formatting is something I'd very much like in Fortran. I think it may help some newcomers to the language as well because this kind of formatting is more common in other languages. But that's for another proposal. :)
This actually wouldn't be too difficult to implement in a standard library. We'd just write a series of wrappers in C, taking different numbers of void*t arguments. We'd then use interoperability to call these from Fortran and wrap them in a generic block. We could have versions accepting between, say, 1 and 30 arguments (tedious, but could be automatically generated), which should be enough for anyone.
I went on to comment:
My suggestion of calls to C was specifically for a printf
function. This would avoid the combinatorial explosion because printf
works on void*
data types. These can be passed in from Fortran using a "deferred-type" argument, type(*)
. The interface would look something like this:
void printf_wrapper1(const char *format, void *arg1) {
printf(format, arg1, arg2)
}
void printf_wrapper2(const char *format, void *arg1, void *arg2) {
printf(format, arg1, arg2)
}
interface printf
subroutine printf_wrapper1(format_str, arg1) bind(c)
character(len=1), dimension(*), intent(in) :: format_str
type(*), intent(in) :: arg1
end subroutine printf_wrapper1
subroutine printf_wrapper2(format_str, arg1, arg2) bind(c)
character(len=1), dimension(*), intent(in) :: format_str
type(*), intent(in) :: arg1, arg2
end subroutine printf_wrapper2
end interface printf
The main complication with this is how to convert between Fortran and C strings. It wouldn't be hard to provide wrapper routines which do this for the Format string, but string arguments to printf
could be more of a challenge.