# Does \`protected\` do anything on \`private\` variables?

**URL:** <https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576>\
**Category:** Help\
**Created:** [April 10, 2025, 4:39pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576 "2025-04-10T16:39:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [April 10, 2025, 4:39pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/1 "2025-04-10T16:39:18Z")

</div>

Are there any use cases for `protected` on `private` variables? As far as I can tell, it’s only useful for `public` variables, but doesn’t actually imply `public`. Basically, I was surprised that the following doesn’t work:

```f90
module bar
    implicit none
    private
    integer, protected :: foo
end module bar

module zing
    use bar, only: foo ! Error: `foo` not found in module `bar`
    implicit none
end module zing

```

This seems like it could potentially be a nice little quality of life improvement for the standard, unless maybe there is some scenario when this is useful?

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [April 10, 2025, 6:44pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/2 "2025-04-10T18:44:56Z")

</div>

`protected` has no effect on private entities and does not imply `public`. I’m not sure what you’re asking, since private entities can’t be accessed by use-association at all.

---

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [April 10, 2025, 7:06pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/3 "2025-04-10T19:06:20Z")

</div>

Thanks! I meant “implies” in the sense that in practice it must `public` for it to be useful (i.e. if I see a `protected` variable I know that elsewhere it will be declared `public`, barring a mistake).

Is there any reason why `protected` _couldn’t_ have an implicit `public`?

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [April 10, 2025, 7:48pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/4 "2025-04-10T19:48:13Z")

</div>

> [@zedthree](#):
>
> Is there any reason why `protected` _couldn’t_ have an implicit `public`?

Only that we don’t tend to do implied attributes in Fortran. I think you’d figure it out pretty quickly if you had made a thing private and hadn’t meant to.

---

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [April 11, 2025, 8:14am UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/5 "2025-04-11T08:14:42Z")

</div>

> [@sblionel](#):
>
> Only that we don’t tend to do implied attributes in Fortran.

Well, there are a couple of famous examples! 😃 It does sound like it’s a mistake to have `private, protected`, so doesn’t it make sense to just have `protected` also mean `public`? This seems like a trivial ergonomic win to me!

---

<div class="post-metadata">

**Author:** ![kimala](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/kimala/32/2707_2.png) [@kimala](https://fortran-lang.discourse.group/u/kimala)\
**Post date:** [April 11, 2025, 9:02am UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/6 "2025-04-11T09:02:38Z")

</div>

But why asking for implicit things that you have to go read in the standard or on other references when you can have it clearly stated in the source code?

```fortran
module bar
    implicit none
    private
    integer, public, protected :: foo
end module bar

```

This compile and, to my knowledge, overrides the `private` statement only for foo, and sets it as `public, protected`.

---

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [April 11, 2025, 9:30am UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/7 "2025-04-11T09:30:38Z")

</div>

Sure, but if

```f90
module bar
    implicit none
    private
    integer, protected :: foo
end module bar

```

can only be a mistake (currently), then it’s unnecessary to require `public` as well. `protected` is a strict subset of `public`, so surely it makes more sense for variables to be exactly one of `public/protected/private`? This would be a trivial change to the standard, would break no existing code, and would be an unsurprising change.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [April 11, 2025, 10:13am UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/8 "2025-04-11T10:13:29Z")

</div>

> [@zedthree](#):
>
> can only be a mistake (currently)

It’s not a mistake, currently. It is silly, and questionable (with a compiler message), but not a mistake. Fortran’s long standing preference is not to break conforming code, silly or not. That is my understanding.

---

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [April 11, 2025, 10:32am UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/9 "2025-04-11T10:32:12Z")

</div>

It’s not a compile error, but it _is_ a mistake – it’s clear that the intention is that it _should_ be `public` but isn’t. As we’ve established, there are no uses for `private, protected` variables, therefore at least one of those attributes is a mistake.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [April 11, 2025, 10:58am UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/10 "2025-04-11T10:58:52Z")

</div>

```auto
module bar
    implicit none
    private
    integer, protected :: foo
end module bar
use bar
foo=42
print *,foo
end

```

This conforming code would break if PROTECTED implied PUBLIC, wouldn’t it?

---

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [April 11, 2025, 1:11pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/11 "2025-04-11T13:11:07Z")

</div>

Sure, but I can see two bugs 🙂

---

<div class="post-metadata">

**Author:** ![mEm](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mEm](https://fortran-lang.discourse.group/u/mEm)\
**Post date:** [April 11, 2025, 3:15pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/12 "2025-04-11T15:15:04Z")

</div>

That’s a clear example of why default IMPLICIT typing is evil when it comes to implementing new language features, which end up behaving way different than one would normally (and logically) expect just for the sake of backward compatibility.

---

<div class="post-metadata">

**Author:** ![mEm](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mEm](https://fortran-lang.discourse.group/u/mEm)\
**Post date:** [April 11, 2025, 3:36pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/13 "2025-04-11T15:36:43Z")

</div>

> [@sblionel](#):
>
> Only that we don’t tend to do implied attributes in Fortran.

Considered that the language (still) builds onto the IMPLICIT typing concept, I would not see having implicit attributes as an issue honestly, but more as a coherent approach.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [April 11, 2025, 5:40pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/15 "2025-04-11T17:40:14Z")

</div>

> [@zedthree](#):
>
> Well, there are a couple of famous examples! 😃 It does sound like it’s a mistake to have `private, protected`, so doesn’t it make sense to just have `protected` also mean `public`?

Yes, there are some counter examples, such as implied save with initialization, but that is an example of an implicit attribute that has caused problems and confusion in the language since it was introduced, so we don’t want more of those.

In my opinion, the combination of public+protected when it overrides a private declaration should be explicit. When the private+protected combination occurs, the compiler should at least warn the programmer, and maybe it should even be an error that the compiler is required to report. However, when protected entities are public by default, then the protected attribute alone should not be treated as an error, because to do so would just be a pain in the a`**` with no benefit.

Also, there is the possibility that private+protected could be given a meaning in the future, perhaps in the context of host association in submodules or some other similar feature.

> [@mEm](#):
>
> Considered that the language (still) builds onto the IMPLICIT typing concept,[…]

The only reason that implicit typing is in the language is because of backward compatibility with legacy code. No one has yet proposed a way to eliminate implicit typing while maintaining backwards compatibility that has been accepted by the community or by the standards committee. There is even an ongoing discussion here about the development of tools that allows programmers to eliminate implicit typing in their codes, and those kinds of tools would be a necessary zeroth step before considering the non-backwards-compatible elimination of implicit typing in the language itself.

---

<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:** [April 11, 2025, 6:14pm UTC](https://fortran-lang.discourse.group/t/does-protected-do-anything-on-private-variables/9576/16 "2025-04-11T18:14:42Z")

</div>

> [@zedthree](#):
>
> `protected` is a strict subset of `public`,

Is that so? I’ve always thought of them as **concerning** separate things… The `public`|`private` attributes/statements have to do with whether things can be **accessed** from outside the module. Whereas `protected` has to do with whether things can be **modified** from outside the module.

Assuming there’s an implicit `UNPROTECTED` blanket statement in every module

```fortran
module mod1
    unprotected
    private
    integer, protected, public :: dummy = 0
end module mod1

```

Then `protected` is counteracting the implicit `UNPROTECTED`, but it has nothing to do with counteracting the `PRIVATE` statement (that’s a role of the `PUBLIC` attribute).
