<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
<div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
<div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
I was trying to understand how you tie shared variables to encapsulation and what you meant by something that students later need to unlearn, and I think I understand now. You’re okay with shared mutable state as long as it’s clear that it’s not shared with
 the whole world but only with some explicit unit, and since an implicit class appears to be the whole world, then its fields seem to be globally shared, and that’s what you want to avoid your students learning. Is that correct?
<div class=""><br class="">
</div>
<div class="">Now, we’re not in the business of telling teachers how to teach, and I assume different teachers teach in different ways. Implicit classes are not exclusively a teaching construct, but a natural Java construct — a natural extension of implicit
 modules and packages. Expert programmers may also appreciate implicit classes just as they appreciate JShell and launching source-code programs, both of which are also explicitly motivated, at least in part, by education (see JEPs 222 and 330).</div>
<div class=""><br class="">
</div>
<div class="">That natural Java construct *allows* teachers to teach in the order and style they choose without *forcing* classes on them. Some teachers may teach just basic control flow and (local) variables, perhaps records, and treat List, Map, and Set as
 built-in constructs without teaching any OOP in the first course and without teaching students how they can implement their own Lists etc. Some may choose to show shared variables (even if only to demonstrate their danger) while some may choose not to, although
 I assume everyone will teach constants. If you want to teach mutable fields only after introducing classes, that’s great and you can certainly do that (perhaps while enjoying the enhanced main and postponing static). But I don’t think the language should enforce
 a particular teaching style, where shared mutable state *must* be taught only in the context of classes. Even if you believe that doing otherwise is bad pedagogy, there is nothing that fundamentally ties shared variables to the ability to create class instances. </div>
<div class=""><br class="">
</div>
<div class="">It’s true that we don’t want to cause harm by letting beginners learn something that needs to later be unlearned, but that’s not the case even for those who do learn about mutable fields. If students begin with implicit classes and seem to think
 that fields (if taught them) — or methods for that matter — are shared with the whole world, they invariably later learn that their “whole world” is actually a unit and that big programs are made by composing such units. They inevitably learn that because
 Java offers no other way. They *can’t* create a global variable, nor a global method, because Java simply doesn’t have those. Even if Java were to someday acquire package-level fields and methods (or even, hypothetically, module-level methods and fields),
 it still wouldn’t have a global namespace (not even for classes!). Learning that basic fact doesn’t require any unlearning, just contextualising.</div>
<div class=""><br class="">
</div>
<div class="">So we're giving teachers more freedom than ever before to teach Java in the manner each of them chooses, and I don’t think we’re inflicting any harm in the process. I think that restricting the abilities of implicit classes further forces a particular
 teaching style — though some may consider it the only correct style — and would also be a less natural Java construct and a less enjoyable one for experienced programmers.</div>
<div class=""><br class="">
</div>
<div class="">— Ron<br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 18 Feb 2023, at 07:11, <a href="mailto:forax@univ-mlv.fr" class="">
forax@univ-mlv.fr</a> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="">
<div style="font-family: arial, helvetica, sans-serif; font-size: 12pt;" class="">
<div class=""><br class="">
</div>
<div data-marker="__QUOTED_TEXT__" class="">
<div class="">yes.</div>
<div class="">from my experience, the time to introduce the notion of class is when you start to have shared mutable state. What i do not like with the implicit class proposal is the fact that you can have fields without defining the class around.<br data-mce-bogus="1" class="">
</div>
<div class="">But i think there is a solution.<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">What about the feature being renamed to "implicit record" instead of "implicit class" ? We have no discuss why the container of an implicit "class" has to be a class instead of an annotation, an interface, an enum or a record.<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">Having the container to be an annotation is useless given that an annotation can not have a main.<br data-mce-bogus="1" class="">
</div>
<div class="">If the container is an interface, methods are abstract by default which is not what we want.<br data-mce-bogus="1" class="">
</div>
<div class="">If the container is an enum, then we are closer to the idea of Ron that it is a singleton, especially if the container defined one implicit enum member like "SINGLETON" . An enum can not be inherited and the default constructor is private which
 is are nice properties.<br data-mce-bogus="1" class="">
</div>
<div class="">if the container is a record with no component, it can not be inherited, the constructor is package visible and more importantly to me, a user can not defined instance fields ...<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">I prefer implicit record to implicit class because with a record as container you can not introduce a shared mutable state by error, you have at least to write static in front of the field.<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">I dread about students being able to write code like this<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">  String name;<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">  void setName(String name) { this.name = name; }</div>
<div class="">  void hello() { System.out.println(name); }<br data-mce-bogus="1" class="">
</div>
<div class="">  void main() {<br data-mce-bogus="1" class="">
</div>
<div class="">    setName("Bob");<br data-mce-bogus="1" class="">
</div>
<div class="">    ...<br data-mce-bogus="1" class="">
</div>
<div class="">    hello();<br data-mce-bogus="1" class="">
</div>
<div class="">  }<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">i.e to be able to declare mutable shared state without a class around (@Ron without class encapsulation).</div>
<div class="">At least if the container is a record, "name" in the example above has to be static, from the student POV, an unusual variable.<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">An implicit record has also the advantage that you do not have to introduce the notion of class to explain the notion of implicit container, given that records are far simpler at the beginning than class, having the implicit container being a
 record make sense because it's records all the way down.<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">What do you think about having the implicit container being a record instead of a class ?<br data-mce-bogus="1" class="">
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div>
</body>
</html>