To: J3 J3/18-265 From: Bill Long Subject: Protected components Date: 2018-October-14 References: J3/18-146 Introduction ------------ Traditionally Fortran programers who wanted to encapsulate data and associated procedures that operated on that data used a module. More recently, the alternate approach of using a derived type has become increasingly popular. This is partly due to the ability to more easily extend a derived type and have polymorphic data objects. However, this new approach lacks one of the facilities available to the module approach - the ability to limit modification of specific data objects using the PROTECTED attribute. Use Cases --------- Consider a set of data in a file. For the programmer, the whole description of this data, to be encapsulated in a derived type, includes characterizing information such as, possibly, the name of the file, descriptive information, and the size of the data set. This auxiliary information would be stored in components of an object of the derived type, but should be defined only by the associated module procedure that sets up the values as part of reading the data from the file. These characterization components should be protected from modification by actions outside the module. Feature suggestion and possible issues -------------------------------------- Allow PROTECTED as an attribute on data components of a derived type definition that appears in a module, with the same general semantics of the existing PROTECTED attribute for data objects. Possible issue: What happens if a component added by type extension is declared with the PROTECTED attribute? The simplest rule would be to permit definition of such components only in a procedure contained in the module where the extended type is defined. Possible issue: Can a value be specified for a component with the PROTECTED attribute in a type constructor expression that appears outside the module? I would propose that this be disallowed. Possible issue: Can PROTECTED components be defined as part of intrinsic assignment to an object of a type with PROTECTED components? I would propose that this be allowed. But, if the assignment is defined assignment, then the defined assignment procedure has to be contained in the module where the type is defined. (Rough) Requirements -------------------- Allow the PROTECTED attribute to be specified in the declaration of a data component in a type definition that appears in a module. Any object of that type will have components with the PROTECTED attribute for components declared with that attribute, independent of whether the whole object has the PROTECTED attribute. Allow explicit modification of the PROTECTED data component only in procedures contained in the module where the derived type definition that includes the component declaration appears. Specify semantics for intrinsic and defined assignment where the variable is a derived type with a component with the PROTECTED attribute. Specify whether a type value constructor for such a type may specify a value for the PROTECTED component.