# Scala simplification and access modifiers

**URL:** <https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104>\
**Category:** Language Design\
**Created:** [April 28, 2019, 8:45am UTC](https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104 "2019-04-28T08:45:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichType](https://contributors.scala-lang.org/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@RichType](https://contributors.scala-lang.org/u/RichType)\
**Post date:** [April 28, 2019, 8:45am UTC](https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104/1 "2019-04-28T08:45:34Z")

</div>

So there seems to be quite an emphasis at the moment on simplifying Scala, getting rid of unnecessary complication. What about reducing the number of access categories to 2 and reducing the number of access modifiers to 1? So there would just be public and private[this]. But private[this] would be renamed private. So “private” would be the only access modifier, but it would have the same meaning as private[this] under the present schema.

I very occasionally use protected, but at least for my code, I don’t really feel the feature carries its weight and I would be willing to sacrifice it in the cause of simplicity. A less radical proposal would be to keep 3 categories:

Unmodified default =\> public  
protected =\> protected[this]  
private =\> private[this]

---

<div class="post-metadata">

**Author:** ![Sciss](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sciss/32/52_2.png) [@Sciss](https://contributors.scala-lang.org/u/Sciss)\
**Post date:** [April 28, 2019, 9:01am UTC](https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104/2 "2019-04-28T09:01:32Z")

</div>

I’d appreciate `private` meaning `private[this]`, I have never seen a case where you want `private` in Scala, you just often write `private` because `private[this]` is ugly and verbose.

---

<div class="post-metadata">

**Author:** ![RichType](https://contributors.scala-lang.org/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@RichType](https://contributors.scala-lang.org/u/RichType)\
**Post date:** [April 28, 2019, 9:27am UTC](https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104/3 "2019-04-28T09:27:07Z")

</div>

Actually thinking about it, I think protected[this] is the most useful access condition after public. I doubt there are many situations where private[this] really gives you significant advantage over protected[this]. So I would change my proposal to a single access modifier:

Unmodified default =\> public  
private =\> protected[this]

---

<div class="post-metadata">

**Author:** ![Sciss](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sciss/32/52_2.png) [@Sciss](https://contributors.scala-lang.org/u/Sciss)\
**Post date:** [April 28, 2019, 9:29am UTC](https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104/4 "2019-04-28T09:29:48Z")

</div>

You need `private` because you don’t want an **API that becomes binary (MiMa) or source incompatible** at the slightest internal change. You need `protected` for trait mixins composition of implementations. So I think the list is

- `public`
- `protected` (I don’t mind if its `protected[this]` by default, but you don’t gain much, so better leave it open)
- `private[this]` - which should thus be called `private`

You could also say you don’t gain much from `private` to `private[this]` change, but AFAIK you need the latter for a `val` to become an actual field in the byte code (correct me if I’m wrong).

---

<div class="post-metadata">

**Author:** ![jducoeur](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jducoeur/32/32_2.png) [@jducoeur](https://contributors.scala-lang.org/u/jducoeur)\
**Post date:** [April 28, 2019, 6:10pm UTC](https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104/5 "2019-04-28T18:10:05Z")

</div>

Um – I use package-private almost exclusively. It’s basically the only time I don’t just leave things public; it’s how I distinguish between public and internal APIs in a library or subsystem. So I _really_ don’t agree here…

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [April 28, 2019, 6:24pm UTC](https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104/6 "2019-04-28T18:24:26Z")

</div>

I second @jducoeur on this one.  
It’s particularly useful when you need to expose some fiddly isternal logic to the tests, but don’t want it part of the exposed API

---

<div class="post-metadata">

**Author:** ![Sciss](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sciss/32/52_2.png) [@Sciss](https://contributors.scala-lang.org/u/Sciss)\
**Post date:** [April 28, 2019, 8:38pm UTC](https://contributors.scala-lang.org/t/scala-simplification-and-access-modifiers/3104/7 "2019-04-28T20:38:43Z")

</div>

But you mean by package private, something like `private[foo]`, no? Just to clarify, I am not suggesting to remove that (and I guess in that and other respects I don’t share the proposal of the OP), and I use that too, indeed. Just saying that `private` with access across different instances of the same type is quite an esoterical use case IMO.

**Edit:** Of course, if the compiler could automatically treat `private` as `private[this]` when applicable (it could determine it from the exact same compile unit, I guess?), then one wouldn’t need to change anything from the programmer’s point of view.
