Skip to content

Commit 3cecdd9

Browse files
committed
Add proposal for protected attribute in derived types
1 parent bfc535f commit 3cecdd9

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

proposals/protected_attribute.txt

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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. By allowing for a direct read-only access, one could enhance
32+
efficiency in those case without sacrificing the robustness of data
33+
encapsulation and hiding.
34+
35+
This proposal suggests to allow the "protected" attribute already used for
36+
module variables being used for derived type components as well. It would also
37+
remedy the asymmetry between the attributes "private"/"public" and the attribute
38+
"protected", as the former two can be applied to both, module variables and
39+
derived type components, while the latter only for module variables.
40+
41+
42+
3. Use Cases
43+
44+
Derived types storing large amount of data could enable read-only access to
45+
components without the necessity of a getter routine and the connected copy
46+
operation:
47+
48+
module data_m
49+
implicit none
50+
51+
type :: prot_t
52+
! Large array component
53+
integer, allocatable, protected :: array(:)
54+
contains
55+
procedure :: set
56+
end type prot_t
57+
58+
contains
59+
60+
subroutine set(this, array)
61+
type(prot_t), intent(out) :: this
62+
63+
this%array = array
64+
65+
end subroutine set
66+
67+
end module data_m
68+
69+
70+
71+
program use_data
72+
use data_m
73+
implicit none
74+
75+
type(prot_t) :: storage
76+
integer, allocatable :: large_array(:)
77+
78+
! Filling up and allocating the large array
79+
! ...
80+
81+
! Storing the large array in the derived type instance
82+
call storage%set(large_array)
83+
84+
! Accessing large array stored in the derived type directly
85+
! No getter routine and no copy necessary
86+
! Dummy argument of the called routine must be intent(in)
87+
call some_routine_processing_but_not_changing_the_array(storage%array)
88+
89+
end program use_data

0 commit comments

Comments
 (0)