-
Notifications
You must be signed in to change notification settings - Fork 15
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
Allow to chain the % operator #317
Comments
Technically, In other languages whose functions (unlike Fortran's) can return references to objects, it is a natural and readable idiom to write something like Fortran differentiates between subroutines and functions. In "chained" usage as above, neither a subroutine call nor a function reference seems to apply -- the procedure components or TBPs being called are returning results, but the last result isn't being consumed by an expression. I suppose that a derived type with such an API could have a Fortran can return a pointer from a function, and the result of a function returning an object pointer can be used as a For functions returning non-pointer derived types, being able to apply a procedure component or TBP reference to their results would obviously be feasible, but that would be applicable only to "stateless" functional values due to the lack of object references. References to data components of function results (whether procedure components or TBPs or not) would also be useful and natural -- it's tiresome to try to explain that |
This is certainly one of the most needed fixes in Fortran. Quite unbelievable that it was rejected.
It would, just that it would generate copies of the object, and the last copy would need to be assigned to another variable. This would still be immensely practical for many cases. Clean code is typically not used in performance-critical sections but to build elegant interfaces, so producing copies in this case would not be a big issue. For example:
Just like one would write
So original Of course, this only makes sense for functions. I do not see how it could work for subroutines. |
In addition to the simpler option of module m
type :: t
integer :: n = 0
contains
procedure :: set
procedure :: launch
end type
contains
function set( this ) result(r)
class(t), intent(in) :: this
type(t) :: r
r%n = this%n + 1
end function
function launch( this ) result(r)
class(t), intent(in) :: this
type(t) :: r
r%n = this%n + 41
end function
end module
use m
type(t) :: object
associate ( o => object%set() )
object = o%launch()
end associate
print *, "object%n = ", object%n, "; expected is 42"
end
So the issue then becomes one of convincing the implementors of the value of introducing facilities in the language that eases matters for the practitioners. Possibly the Community will get behind |
This was proposed in https://j3-fortran.org/doc/year/18/18-134.txt, but rejected.
What were the arguments against? Let's document it here.
This was recently re-requested in https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members.
The text was updated successfully, but these errors were encountered: