-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Additional format specification languages #340
Comments
I'm not sure about this. I think that format specification in Fortran is different but works quite well. What I felt Fortran really needed in the past was the I might be missing something, though. Is there a "feature" advantage in adopting C or Python like format specifiers (i.e. something that Fortran cannot do or does in an inconvenient way)? |
Related: #19 I like and support this idea (whether C- or Python-style, or both). I agree that Fortran formatting works well, but alternative format specifications could be helpful to newcomers who are familiar with some other language. |
I think the easiest way to enjoy the best of both worlds is to have a converter function, e.g. given the following C format string
it should produce the Fortran equivalent
This way you could easily inline it:
Addendum: I imagine regex would be the way to do this by searching for patterns of |
Thinking more about it, probably there is also the advantage that C/C++/Python format specification is more compact and avoids the (potentially annoying) typical combination of single and double quotes. +1 also for me. |
I see now that my previous idea to directly mimic the Instead we just need these format specifier "adaptors", e.g. the C++ code:
would be in Fortran
One down-side is that since this would not be a built-in function, syntax errors in the format specifier or the number of arguments can't be caught at compile time. Instead, the program terminate at runtime with a potentially cryptic error message. To avoid this the I'd still argue that having a C-style @certik, do you think this is worth prototyping in LFortran at some point? What I mean is the proposed |
Fortran may not support variadic routines, but you can sort-of work around
that, see for instance
https://sourceforge.net/p/flibs/svncode/HEAD/tree/trunk/tests/strings/test_keyvars.f90.
That said, one of the major drawbacks IMHO of C-style formats is the
impossibility to group them and to have repetition:
write(*, '(10(a,i5)' ) ( string(i), value(i) , i=1,100)
for instance would have to be done using an explicit do-loop in C and some
logic to add a newline at the right moment. Well, just my pet peeve :).
Op di 16 mrt. 2021 om 10:43 schreef Ivan Pribec ***@***.***>:
… I see now that my previous idea to directly mimic the printf (C) or format
(C++) is not well-suited to Fortran due to absence of variadic functions.
Instead we just need these format specifier "adaptors", e.g. the C++ code:
std::cout << std::format("Hello {}!\n", "world");
would be in Fortran
use iso_fortran_env, only: stdout => output_unit
write(stdout, format("Hello {}!\n")) "world" ! obviously, this is a contrived example
One down-side is that since this would not be a built-in function, syntax
errors in the format specifier or the number of arguments can't be caught
at compile time. Instead, the program terminate at runtime with a
potentially cryptic error message. To avoid this the format function
would need to validate it's input first, and terminate with a helpful
message, making it an impure function. Still there would be no way to
protect against mismatch in the number of arguments. One could perhaps
overcome these issues by making format a preprocessor macro, but this would
again make it a non-portable solution.
I'd still argue that having a C-style format function would be welcome
and make Fortran I/O easier in some situations. It would however be mostly
the responsibility of the caller, to get the C formatting string right. For
a programmer familiar with C or Python format specifiers, a few
*edit-compile-run* cycles might be easier than learning the Fortran
format specificiations.
@certik <https://github.com/certik>, do you think this is worth
prototyping in LFortran at some point? What I mean is the proposed format
function would still be a (non-standard) stdlib thing, but LFortran would
offer compile-time format syntax checking. This would imply the compiler
gives stdlib some type of elevated status.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#340 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN6YR6OOTUERPWALO5JGK3TD4R4FANCNFSM4ZDXOZNQ>
.
|
That is a great feature of Fortran indeed. I have been using the "infinite list" specifiers, such as |
Since this seems to require quite a bit of work regardless, why don't we aim at combining the best of both worlds, i.e. combining the "compactness" of C/Python style and the group repetitions of Fortran? I have found an excellent comment by @14NGiestas that might be used as a starting point.
If this can be achieved with PS: just realised that @ivan-pi already used even the same expression ("best of both worlds") in a previous comment above, sigh, sorry for the repetition. |
Why not take this one step further:
Instead of converting/substituting values from left to right, there might
be format specifiers that put the values in a specific position. This could
be handy for intenationalisation. For instance:
write(*, fmt) x, y
could then result in a text "value of x is y" OR "y is the value of x", if
the (natural) language requires a different order. IIRC, Java allows that
and with the variadic workaround I posted, that might be relatively easy to
implement.
Op di 16 mrt. 2021 om 11:47 schreef Emanuele Pagone <
***@***.***>:
… Since this seems to require quite a bit of work regardless, why don't we
aim at combining the best of both worlds, i.e. combining the "compactness"
of C/Python style and the group repetitions of Fortran?
I have found an excellent comment
<#19 (comment)>
by @14NGiestas <https://github.com/14NGiestas> that might be used as a
starting point.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#340 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN6YR36BBGFSYVV4U2DBODTD4ZKPANCNFSM4ZDXOZNQ>
.
|
The preprocessing would have to be done on the calling code. I don't think this is a viable option until I found a few more comments in the proposal of @gronki: j3-fortran/fortran_proposals#69, the most relevant being:
I guess we will need to adopt a grammar (and use it to automatically create a parser), but it might be good to create a toy implementation first (with limited support of strings, integers, and reals) just to experiment with syntax. So the Python formatting syntax begins by defining "replacement fields" which are the text portions surround by curly braces
The
Since the result of the stdlib Addendum: positional arguments can be pursued with Arjen's workaround, but I guess this involves a custom write subroutine. I learn that Intel Fortran actually supports some form of named "variable" interpolation, limited to format strings as one its extensions. With this extension you can do things such as:
|
Formatting and pretty printing output is a usual task in any programming language. The Fortran format specifiers are a bit special because they are quite unique compared with other programming languages with C-like or Python-like format specifier. While Fortran's format specifiers are probably older than the C-like or Python-like format specifiers the later found wide adoption across several programming languages.
This raises the question if stdlib could offer a
stdlib_format
module to allow formatting of strings using C-like and/or Python-like format specifiers.Original post by @ivan-pi in #337 (comment):
The text was updated successfully, but these errors were encountered: