[J3] [EXTERNAL] Can a type-bound procedure be passed as an actual argument
Malcolm Cohen
malcolm at nag-j.co.jp
Thu Jan 12 01:44:06 UTC 2023
> Tom is correct, it is permitted by virtue of the syntax not being allowed.
“permitted” -> “prohibited”.
(I was choosing between “prohibited” instead of “not permitted”, and somehow managed to omit the “not” but write the wrong word. Sigh.)
Cheers,
--
..............Malcolm Cohen, NAG Oxford/Tokyo.
From: J3 <j3-bounces at mailman.j3-fortran.org> On Behalf Of Malcolm Cohen via J3
Sent: Thursday, January 12, 2023 9:56 AM
To: 'General J3 interest list' <j3 at mailman.j3-fortran.org>
Cc: Malcolm Cohen <malcolm at nag-j.co.jp>
Subject: Re: [J3] [EXTERNAL] Can a type-bound procedure be passed as an actual argument
Tom is correct, it is permitted by virtue of the syntax not being allowed.
Type-bound procedures are invoked through an object. You can’t do that unless you have the object. Dynamic dispatch needs the object!
If the writer of the module wants to let you pass a procedure as an actual argument, he makes the procedure name public. That’s it.
It’s the same with generics (which are static dispatch); if the specific procedure name is not public, you cannot pass it as an actual argument.
If you want to do some kind of de-objectified callback (perhaps you had an argument with the module author and he refused to make the procedure public?), you can define an internal procedure and pass that as an actual argument. If there is no passed-object dummy argument, the internal procedure will need access to the object (by host association) so it can invoke through it. If there is a passed-object dummy argument, the internal procedure must have the object as an actual argument, and can thus invoke the type-bound procedure.
i.e. instead of
CALL sub1(obj%tbp,…)
you do
CALL sub1(my_obj_tbp,…)
with
SUBROUTINE my_obj_tbp(“extra args if there are any”)
CALL obj%tbp(“extra args if there are any”)
END SUBROUTINE
Yes, this is more verbose than the current-invalid single call, but at least it is a lot more explicit about what is going on, and generalises to handle the passed-object case correctly, even when “sub1” is going to invoke the proc with a different object.
Cheers,
--
..............Malcolm Cohen, NAG Oxford/Tokyo.
From: J3 <j3-bounces at mailman.j3-fortran.org <mailto:j3-bounces at mailman.j3-fortran.org> > On Behalf Of Clune, Thomas L. (GSFC-6101) via J3
Sent: Thursday, January 12, 2023 3:59 AM
To: General J3 interest list <j3 at mailman.j3-fortran.org <mailto:j3 at mailman.j3-fortran.org> >
Cc: Clune, Thomas L. (GSFC-6101) <thomas.l.clune at nasa.gov <mailto:thomas.l.clune at nasa.gov> >
Subject: Re: [J3] [EXTERNAL] Can a type-bound procedure be passed as an actual argument
This is not permitted. (But would be nice!)
R1524 actual-arg is expr
24 or variable
25 or procedure-name
26 or proc-component-ref
27 or conditional-arg
28 or alt-return-spec
29
t1%foo is a binding name which is not in that list.
Real_foo can of course be passed, but it is not directly accessible through t1.
* Tom
From: J3 <j3-bounces at mailman.j3-fortran.org <mailto:j3-bounces at mailman.j3-fortran.org> > on behalf of j3 <j3 at mailman.j3-fortran.org <mailto:j3 at mailman.j3-fortran.org> >
Reply-To: j3 <j3 at mailman.j3-fortran.org <mailto:j3 at mailman.j3-fortran.org> >
Date: Wednesday, January 11, 2023 at 1:08 PM
To: j3 <j3 at mailman.j3-fortran.org <mailto:j3 at mailman.j3-fortran.org> >
Cc: Daniel C Chen <cdchen at ca.ibm.com <mailto:cdchen at ca.ibm.com> >
Subject: [EXTERNAL] [J3] Can a type-bound procedure be passed as an actual argument
Hi All,
Consider the following code
Case 1:
type dt
contains
procedure, nopass :: foo => real_foo
end type
interface
subroutine real_foo()
end
end interface
type(dt) :: t1
call sub1(t1%foo) !!! Is it legal to pass t1%foo as actual argument?
end
Case 2:
module m
type dt
contains
procedure :: foo => real_foo
end type
contains
subroutine real_foo(this)
class(dt) :: this
end
end
program main
use m
type(dt) :: t1
call sub1(t1%foo) !!! Is it legal to pass t1%foo as actual argument?
end
I couldn’t find any wording in the standard (F2018) that prohibits the two usages above, and yet a couple of compilers that I have access to flags error message.
Thanks,
Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20230112/8b7eac4d/attachment-0001.htm>
More information about the J3
mailing list