Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28992 Discussions

Hiding type-bound procedure of a parent data type in child data type

Jon_D
New Contributor II
353 Views

I have a parent and a child data type with a generic type-bound procedures as follows:

TYPE ParentType
      PRIVATE
      ...
CONTAINS
      PROCEDURE,PASS,PRIVATE :: MethodA
      PROCEDURE,PASS,PRIVATE :: MethodB
      GENERIC,PUBLIC                  :: GenMethod     => MethodA             , &
                                                                                 MethodB
END TYPE ParentType

TYPE,EXTENDS(ParentType) :: ChildType
      PRIVATE
      ...
CONTAINS
      PROCEDURE,PASS,PRIVATE :: ChildMethodA
      PROCEDURE,PASS,PRIVATE :: ChildMethodB
      GENERIC,PUBLIC                  :: GenMethod     => ChildMethodA             , &
                                                                                 ChildMethodB
END TYPE ChildType

Procedures MethodA and ChildMethodA have the same signatures. So do MethodB and ChildMethodB. Essentially the idea is to overwrite the parent GenMethod generic procedure in ChildType. However, when I declare a ChildType variable and call ChildType%GenMethod the procedure from the parent type is executed instead of that from the ChildType.

My question: Is it possible to somehow "hide" the parent type's GenMethod so that when a client code uses ChildType it will only be aware of the ChildType's overwriting GenMethod?

Thanks for any help,

Jon

0 Kudos
4 Replies
IanH
Honored Contributor III
353 Views

Not in the general case. Whether this is possible conceptually for limited cases depends on whether the type definitions are in the same module.

If ChildMethodA or ChildMethodB are not distiguishable from MethodA and MethodB then you should get a compile error.  See the rules for C1215 in F2008 for the requirements.  If they are distringuishable, then you are not hiding the existing specific bindings behind the generic, but adding to them.

(If the type definitions are in the same module and the procedure that is going to do the overriding has the same characteristics bar the passed object (per 4.5.7.3) then you can simply override the bindings in the parent type.  If the type definitions are in the same module and the procedure that is going to do the overriding has different characteristcs but is not distinguishable under the generic name rules, then you are in trouble.  If the type definitions are in different modules and the procedure that is going to do the overriding is not distinguishable, then you are in trouble.  It would be nice if the language allowed some method for bindings to be overridable in different modules, but not otherwise accessible.)

0 Kudos
Jon_D
New Contributor II
353 Views

Thanks. The parent and child types and procedures are in different modules. It looks like the best I can do is to give a different name to the overriding procedure, although the "overridden" procedure is not really overridden and still visible to the client code. This is something I was hoping to avoid accidental calls to this procedure.

0 Kudos
Andrew_Smith
Valued Contributor I
353 Views

Where in the standard does in metion that having a different module for the child would make any difference to inheritance of type bound procedures ?  

0 Kudos
IanH
Honored Contributor III
353 Views

Note the specific bindings in the posted code were PRIVATE - my comments were [perhaps a bit too] specific to that.  This doesn't change their inheritance - but it obviously does change their accessibility.  See the last few paragraphs of 4.5.4 in F2008. 

My misinterpretation a few months back (I think it was here, perhaps it was c.l.f) was that inaccessible bindings could be overridden.

0 Kudos
Reply