(j3.2006) Should parent component have the accessibility of the parent type?

Cohen Malcolm malcolm
Mon Jan 25 22:02:47 EST 2016


Hi Daniel,

<<<
Module AB
  private
  Type A_t
    Integer i
  End type
  Type, extends(A_t) :: B_t
  End type
  Public B_t
End module

Module C
  Use AB
  Type, extends(B_t) :: C_t
  End type
Contains
  Subroutine sub(this)
    Type(C_t) :: this
    This%B_t%A_t%i = 4 !! NOT allowed
    This%B_t%i = 4 !! Allowed
  End subroutine
End module

>>>

A_T is private, therefore it is not accessible ouside of AB.

Component I is public therefore it is accessible outside of AB.

So the comments are correct.

>Is there any case that a parent component should not be accessible at all?

I don?t understand this question, since the parent component is already inaccessible in this example.  This is how it is meant to be.  Component I is public so it is accessible, if you want component I not to be public you should specify that!  The accessibility of a type (A_T) and its components (I) are individually specifiable and not connected in any way.

This is useful in a number of scenarios.  For example, when extending via two steps, so that the private extension was internal to the module and not cluttering up the user?s view of the exported extension.  E.g.

Module base
  Type root
  End Type
End Module

Module extension
  Use base
  Type,Extends(root),Private :: mytype
    Integer a,b
  End Type
  Type,Extends(mytype) :: type
    Integer c
  End Type
  ...
End Module

Outside of the module EXTENSION, it looks like type TYPE was produced like this:
  Type,Extends(root) :: type
    Integer a,b,c
  End Type
and that means that ?mytype? does not appear in the component list, so the user can have his own ?mytype? later on, without it conflicting with the EXTENSION module.

There are plausible reasons for wanting to be able to extend a type like this without exporting the internal details of how it was done.  For example, one might want to act on the ?mytype? part as a whole separately from the rest of the added components.  I am sure people can think of other reasons.

Another time when a private parent component is useful is when you are adding private components:

Module E2
  Use base
  Type,Extends(root),Private :: mytype2
    Integer,Private :: mya,myb
  End Type
  Type,Extends(root) :: type
    Integer c
  End Type
  Type(type) :: x
  ...
End Module

If MYTYPE2 were public, then one could act on the private components of X by using X%MYTYPE2.  Having MYTYPE2 private prevents this, so one can only act on X as a whole, or on the public component X%C.

Cheers,
-- 
........................Malcolm Cohen, Nihon NAG, Tokyo.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.j3-fortran.org/pipermail/j3/attachments/20160126/a0cfc646/attachment.html 



More information about the J3 mailing list