1
- To: J3
1
+ To: J3 J3/20-109
2
2
From: Brad Richardson
3
3
Subject: Traits for Types
4
- Date: 2020-January-27
4
+ Date: 2020-February-23
5
5
6
6
Proposal for Fortran Standard: 202y (NOT 202x)
7
7
8
8
1. Problem
9
9
10
10
Currently, in order to pass a derived type to some library expected a type
11
- derived from it's own abstract type requires the addition of a "wrapper" type
12
- to be used. This requires a cumbersome amount of boiler plate code and is
13
- not conducive to the type of "generic" code one would like to be able to write.
11
+ derived from it's own abstract type requires the addition of a "wrapper"
12
+ type to be used. This requires a cumbersome amount of boiler plate code and
13
+ is not conducive to the type of "generic" code one would like to be able to
14
+ write.
14
15
15
- This proposal seeks to address the issue that writing "generic" libraries is
16
- either not possible, or places undue burden on its users. The main benefits are
17
- the reduction in repeated logic or boiler plate wrapper types required for
18
- generic libraries, thereby enabling more code reuse and easier to maintain
19
- code bases.
16
+ This proposal seeks to address the issue that writing "generic" libraries
17
+ is either not possible, or places undue burden on its users. The main
18
+ benefits are the reduction in repeated logic or boiler plate wrapper types
19
+ required for generic libraries, thereby enabling more code reuse and easier
20
+ to maintain code bases.
20
21
21
22
2. Proposed Solution
22
23
23
24
The solution would require the addition of a few attributes and variable
24
- declaration capabilities. First is the addition of the `trait` attribute
25
- to a type specification. This requires that the type be abstract, and contain
25
+ declaration capabilities. First is the addition of the `trait` attribute to
26
+ a type specification. This requires that the type be abstract, and contain
26
27
no components. I.e.
27
28
28
29
type, abstract, trait :: Show_t
@@ -37,10 +38,10 @@ no components. I.e.
37
38
end function show_i
38
39
end interface
39
40
40
- Second is the addition of the `implements` attribute for a type specification.
41
- The `implements` attribute requires a list of at least one trait the type is to
42
- implement, requiring the type to contain the procedures with the interfaces
43
- defined by the trait(s). I.e.
41
+ Second is the addition of the `implements` attribute for a type
42
+ specification. The `implements` attribute requires a list of at least one
43
+ trait the type is to implement, requiring the type to contain the
44
+ procedures with the interfaces defined by the trait(s). I.e.
44
45
45
46
type, implements(Show_t) :: Thing_t
46
47
character(len=:), allocatable :: the_string
@@ -55,13 +56,14 @@ defined by the trait(s). I.e.
55
56
string = self%the_string
56
57
end function show
57
58
58
- Third is the addition of a variable declaration form using `trait`, in a similar
59
- fashion to `class`. The `trait` specification would require a list of at least
60
- one trait, and must either be allocatable, or a dummy argument of a procedure.
61
- In a similar manner that the instantiated value of a class variable must be of
62
- that type, or an extended type, the instantiated value of a trait variable must
63
- be of a type which implements the given trait(s). Thus, no components of the
64
- instantiated value are accessible, only the procedures defined by the trait(s).
59
+ Third is the addition of a variable declaration form using `trait`, in a
60
+ similar fashion to `class`. The `trait` specification would require a list
61
+ of at least one trait, and must either be allocatable, or a dummy argument
62
+ of a procedure. In a similar manner that the instantiated value of a class
63
+ variable must be of that type, or an extended type, the instantiated value
64
+ of a trait variable must be of a type which implements the given trait(s).
65
+ Thus, no components of the instantiated value are accessible, only the
66
+ procedures defined by the trait(s).
65
67
66
68
3. Backward compatibility
67
69
@@ -73,3 +75,5 @@ compatibility.
73
75
74
76
Online discussion that led to this proposal can be found at
75
77
https://github.com/j3-fortran/fortran_proposals/issues/125
78
+
79
+
0 commit comments