I don’t to write getter and setter subrountine
Could you elaborate a bit? Why do you want protected? Can private not do the job?
Your question, or rather statement, does not provide much information about the problem you trying to solve and that causes us to guess. Which may or may not turn out to be useful.
Just like @Arjen said, please elaborate.
But keep in mind that, unlike other programming languages, in Fortran the public
|protected
|private
attributes belong to the module, not to the derived type.
Not entirey:
module privatetype
implicit none
type :: mytype
private
integer :: x
end type mytype
end module
program test_privatetype
use privatetype
type(mytype) :: y
y%x = 1
end program test_privatetype
This fails because of the direct access to component x.
When it comes to avoiding a “getter” procedure, I suspect what @weixing1531 would like to do is to provide direct read-only access to a component
type :: mytype
private
integer, protected :: x
end type
There was a proposal several years ago to do exactly that, but as I recall there was sharp disagreement in the committee about (I think) how that would behave when extending the type, and the whole thing got scuttled.
There might be issues with the structure constructor of an (outside the base module) extended type —e.g., when the base type is abstract and/or the protected component has no default initialization.
But the accessor pattern (imo) is just a Java thing and if you only need the getter but not the setter, or vice versa, then the component should just be either public or a constant/parameter.
(The Go style guide actually suggests using it only when absolutely necessary, and even then, avoiding the “Get” prefix in the getter.)
This page seems to have a lot of related info. I also think this feature would be useful if some components are not to be modified via direct component access.
To “emulate” this feature, I typically attach an underscore at the end of component names (e.g., “integer :: foo_”) so that I can recall that I should not modify this component directly (but rather I should use a proper setter routine). In my case, derived types are given a type-bound procedure set()
, which handles the assignment of all such variables via keyword arguments (in order to perform any additional things associated with each assignment).
I had completely forgotten about that thread; thanks for linking it!