# Why can't a type component be protected?

**URL:** <https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864>\
**Category:** Uncategorized\
**Created:** [June 19, 2025, 4:02am UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864 "2025-06-19T04:02:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![weixing1531](https://avatars.discourse-cdn.com/v4/letter/w/a88e57/32.png) [@weixing1531](https://fortran-lang.discourse.group/u/weixing1531)\
**Post date:** [June 19, 2025, 4:02am UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/1 "2025-06-19T04:02:15Z")

</div>

I don’t to write getter and setter subrountine

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [June 19, 2025, 6:50am UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/2 "2025-06-19T06:50:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jwmwalrus](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jwmwalrus/32/4483_2.png) [@jwmwalrus](https://fortran-lang.discourse.group/u/jwmwalrus)\
**Post date:** [June 19, 2025, 8:00am UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/3 "2025-06-19T08:00:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [June 19, 2025, 11:27am UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/4 "2025-06-19T11:27:17Z")

</div>

Not entirey:

```fortran
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

```

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [June 19, 2025, 11:27am UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/5 "2025-06-19T11:27:39Z")

</div>

This fails because of the direct access to component x.

---

<div class="post-metadata">

**Author:** ![nncarlson](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nncarlson/32/1175_2.png) [@nncarlson](https://fortran-lang.discourse.group/u/nncarlson)\
**Post date:** [June 19, 2025, 5:40pm UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/6 "2025-06-19T17:40:39Z")

</div>

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

```fortran
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.

---

<div class="post-metadata">

**Author:** ![jwmwalrus](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jwmwalrus/32/4483_2.png) [@jwmwalrus](https://fortran-lang.discourse.group/u/jwmwalrus)\
**Post date:** [June 19, 2025, 7:33pm UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/7 "2025-06-19T19:33:20Z")

</div>

> [@nncarlson](#):
>
> 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,

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.)

---

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [June 20, 2025, 8:34pm UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/8 "2025-06-20T20:34:51Z")

</div>

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.

> [@Protected attribute for derived type components](https://fortran-lang.discourse.group/t/protected-attribute-for-derived-type-components/7360):
>
> Back in 2021 specifications and requirements were passed by the standards committee for adding a protected attribute for derived type components. All that remained were edits to the standard. This was to appear in the 2023 standard, but it’s not there (that I can find), nor is it even in the list for 202y. Does anyone know what happened? Zach Jibben was the shepherd of this proposal, but he left the committee a year or so ago. He pointed me to papers 21-168 and 21-169r2 [here](https://j3-fortran.org/doc/meeting/224).

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).

---

<div class="post-metadata">

**Author:** ![nncarlson](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nncarlson/32/1175_2.png) [@nncarlson](https://fortran-lang.discourse.group/u/nncarlson)\
**Post date:** [June 20, 2025, 10:16pm UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/9 "2025-06-20T22:16:57Z")

</div>

I had _completely_ forgotten about that thread; thanks for linking it!

---

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [June 21, 2025, 1:07pm UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/10 "2025-06-21T13:07:49Z")

</div>

> [@jwmwalrus](#):
>
> 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.)

Just out of interest, does the above caveat about “accessor pattern” also refer to my use case (where I want to “read” a field/component directly with no potential overhead, while requiring the use of a setter to guarantee any additional stuff to be done for assignment)? I remember it is not generally recommended to write “setters/getters” to all fields of a class as an attempt of encapsulation (like in old Java or OO books), but the above “asymmetric” need for direct read of a field + setter routine seems more common and reasonable to me (for performance + maintainance reasons etc).

> [@weixing1531](#):
>
> I don’t to write getter and setter subrountine

In my case, even when the performance is not a problem, I do not want to write getter/setter routines in many cases either, simply because it is _tedious_ to write module procedures for that purpose (as compared to other languages where one-liners are often possible).

---

<div class="post-metadata">

**Author:** ![jwmwalrus](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jwmwalrus/32/4483_2.png) [@jwmwalrus](https://fortran-lang.discourse.group/u/jwmwalrus)\
**Post date:** [June 21, 2025, 11:55pm UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/12 "2025-06-21T23:55:10Z")

</div>

There’s nothing wrong with the accessor pattern. But it’s often overused unnecessarily.

If the component cannot easily accommodate to value changes (e.g., if it’s an int enum, must be unsigned, must lie within a given range, etc.), then having a setter is fine. Although, just doing a sanity check when that value is actually used in the code, and returning an error, is even better (imho).

(I suppose Fortran 2023 `enumerator types`, and the not-yet-standardized `unsigned` can help a little bit.)

Your single-setter with optional args is actually a nice touch, since it avoids the overhead common in languages where chaining is allowed.

---

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [June 22, 2025, 7:39pm UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/13 "2025-06-22T19:39:28Z")

</div>

> [@jwmwalrus](#):
>
> There’s nothing wrong with the accessor pattern. But it’s often overused unnecessarily.

Thanks for your comments! I also read web articles about this trend (for old OO codes and Java), and I was wondering about the recommendation of the Go community(?) about accessors in general, because it may depend on the culture (? or history) of the language.

> [@jwmwalrus](#):
>
> If the component cannot easily accommodate to value changes

In my case they are simulation parameters and some data, and they are associated with other params/data, so any change should be done consistently (with additional changes of other data and checks if necessary). I feel derived types + setters are convenient for this kind of self management.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [June 23, 2025, 12:57pm UTC](https://fortran-lang.discourse.group/t/why-cant-a-type-component-be-protected/9864/14 "2025-06-23T12:57:44Z")

</div>

> [@septc](#):
>
> In my case, even when the performance is not a problem, I do not want to write getter/setter routines in many cases either, simply because it is _tedious_ to write module procedures for that purpose (as compared to other languages where one-liners are often possible).

Getters are most of time unnecessary. Setters are sometimes justified. And that’s where a `protected` attribute would have been useful.
