Skip to content

Commit 771f01a

Browse files
authored
Merge pull request #31 from aradi/protected_attribute
Add proposal for protected attribute in derived types
2 parents f4d5747 + cf08836 commit 771f01a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

proposals/protected_attribute.txt

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
To: J3
2+
From: Balint Aradi
3+
Subject: Protected attribute for derived type components
4+
Date: 2019-October-21
5+
6+
Proposal for Fortran Standard: 202y (NOT 202x)
7+
8+
9+
1. Introduction
10+
11+
The proposal is to allow specify the protected attribute for components in
12+
derived types. Example:
13+
14+
15+
type :: prot_t
16+
integer, allocatable, protected :: array(:)
17+
end type prot_t
18+
19+
20+
2. Motivation
21+
22+
Data hiding using derived types with private components allows programmer to
23+
provide robust types, where internals can only be changed in controlled
24+
fashion. The usual implementation is to provide setter and getter routines for
25+
writing and reading components. Both operations involve copying the passed data,
26+
which can be inefficient when storing large arrays in the derived type
27+
instance. While in the setter routine the copying serves robustness by
28+
"disentangling" the data stored in the derived type from the original data, the
29+
getter routine and the connected copy operation might be superfluous, when the
30+
consumer only wants to read but not modify the data stored in the derived type
31+
intance.
32+
33+
By allowing for a direct read-only access, one could enhance efficiency in those
34+
cases without sacrificing the consistency of the encapsulated data. Direct
35+
access is possible with current Fortran already, but only if the respective
36+
derived type component is public. This would jeopardize data consistency,
37+
though, as a consumer could change the respective component arbitrarily, without
38+
using the provided setter routine.
39+
40+
This proposal suggests to allow the "protected" attribute already used for
41+
module variables being used for derived type components as well. It would also
42+
remedy the asymmetry between the attributes "private"/"public" and the attribute
43+
"protected", as the former two can be applied to both, module variables and
44+
derived type components, while the latter only for module variables so far.
45+
46+
47+
3. Use Cases
48+
49+
Derived types storing large amount of data could enable read-only access to
50+
components without the necessity of a getter routine and the connected copy
51+
operation:
52+
53+
module data_m
54+
implicit none
55+
56+
type :: prot_t
57+
! Large array component
58+
integer, allocatable, protected :: array(:)
59+
contains
60+
procedure :: set
61+
end type prot_t
62+
63+
contains
64+
65+
subroutine set(this, array)
66+
type(prot_t), intent(out) :: this
67+
68+
this%array = array
69+
70+
end subroutine set
71+
72+
end module data_m
73+
74+
75+
76+
program use_data
77+
use data_m
78+
implicit none
79+
80+
type(prot_t) :: storage
81+
integer, allocatable :: large_array(:)
82+
83+
! Filling up and allocating the large array
84+
! ...
85+
86+
! Storing the large array in the derived type instance
87+
call storage%set(large_array)
88+
89+
! Accessing large array stored in the derived type directly
90+
! No getter routine and no copy necessary
91+
! Dummy argument of the called routine must be intent(in)
92+
call some_routine_processing_but_not_changing_the_array(storage%array)
93+
94+
! Inconsistent change, consumer is supposed to call set() to change
95+
! encapsulated data.
96+
! Uncommenting the next line should trigger a compiler error.
97+
!storage%array(1) = -1
98+
99+
end program use_data

0 commit comments

Comments
 (0)