Skip to content
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

Simplify the function bool2str in main.f90 #1

Open
Beliavsky opened this issue Sep 5, 2024 · 0 comments
Open

Simplify the function bool2str in main.f90 #1

Beliavsky opened this issue Sep 5, 2024 · 0 comments

Comments

@Beliavsky
Copy link

It can be written more concisely as

    function bool2str(bool) result(str)
        logical, intent(in) :: bool
        character(len=:), allocatable :: str
        if (bool) then
            str = '.true.'
        else
            str = '.false.'
        end if
    end function bool2str

With gfortran the output of the code

module m
implicit none
contains
    function bool2str(bool) result(str)
        logical, intent(in) :: bool
        character(len=:), allocatable :: str
        if (bool) then
            str = '.true.'
        else
            str = '.false.'
        end if
    end function bool2str
end module m

program main
use m
implicit none
print "(a)", "'" // bool2str(.true.) // "'"
print "(a)", "'" // bool2str(.false.) // "'"
end program main

is

'.true.'
'.false.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant