Suppose we have a procedure that takes a polymorphic assumed-rank object and returns its length type parameter if it is of type character, otherwise -1.
The ideal solution would be the following:
integer function getCharLen(obj) result(length)
class(*), intent(in) :: obj(..)
select type (obj)
type is (character(*))
length = len(obj)
class default
length = -1
end select
end function getCharLen
Surprisingly, that simple solution is not Fortran standard-compliant. Instead, the following is the solution:
integer function getCharLen(obj) result(length)
class(*), intent(in) :: obj(..)
length = -1
select rank (obj)
rank (0)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (1)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (2)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (3)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (4)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (5)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (6)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (7)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (8)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (9)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (10)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (11)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (12)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (13)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (14)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank (15)
select type (obj)
type is (character(*))
length = len(obj)
end select
rank default
error stop "getCharLen(): unsupported rank."
end select
end function getCharLen
Why is there a need for such redundancy? The current solution isn’t future-proof if future standards allow a higher maximum array rank.
program main
implicit none
character(len=5) :: s
integer :: n
s = "hello"
n = 1
print *, getCharLen(s)
print *, getCharLen(n)
contains
integer function getCharLen(obj) result(length)
class(*), intent(in) :: obj(..)
select type (obj)
type is (character(*))
length = len(obj)
class default
length = -1
end select
end function getCharLen
end program
You get:
$ lfortran a.f90
5
-1
@shahmoradi if you have open source (or even closed sourced) codes that don’t work yet with LFortran, just send them my way, I’ll fix it.
kudos to LFortran team!
Unfortunately, even if LFortran accepts the syntax, that would be against other compilers’ behavior and the Fortran standard. Type, kind, and rank are orthogonal in Fortran, so I don’t see any obvious reason the first elegant solution would break anything in the language standard.
Here is the behavior with other compilers and older LFortran versions:
I will try to submit a proposal to the Fortran committee on GitHub about lifting this restriction. If you know a better venue for submitting a proposal, or would like to take the lead on this if you see value in it, I’d appreciate it.
Well, I worry about things that I can change, and the above things I cannot change, so I don’t worry about it.
My next goal is to get wide enough acceptance of LFortran that if LFortran supports it, it will be enough, because it will on its own put pressure on other compilers and the committee to standardize it. We are not there yet, but working hard. What other features do you need that currently keep you with other compilers?
(MR-3) A polymorphic assumed-rank argument shall be acceptable as the
selector in a SELECT TYPE statement. Within the construct, the
associate names are all assumed-rank.
I searched the GitHub page for fortran proposal and found a closed issue by Steve Kargl with same issue and it seems one still need a select rank construct but can be written separately than the select type.
A feature I need that lfortran doesn’t yet have though it was in Fortran 90 is select_real_kind(n) even if n>precision(1d0). I test my programs with various compilers to catch both my errors and compiler issues, and that feature is useful when compilers have different numbers of real kinds, e.g. older lfortran versions had 2, ifx and AMD flang have 3, and g95 and gfortran have 4. It also helps if kind(x) is not the number of bytes in x, e.g. NAG Fortran years ago (and still? - I don’t now have access to it.)
It would be very helpful if someone from the committee could explain why len() should not accept an assumed rank actual argument, when it seemingly does not deal with argument rank (unless len is internally stored as part of shape and rank information).
Not sure what “randomize it as an option” means, but my code does not rely on 4/8 etc kind numbers. I tried to add a comment to @certik’s lfortran issue #17338 because I was discussing not a general point about Fortran but whether a problem in lfortran has been fixed. But that issue has now been closed; I intend to raise a new one.
The important general point is that real(n):: x won’t compile if n is not a valid real kind, but selected_real_kind(p) compiles and runs whether the positive nteger p is a valid real precision or not.