Implicit Record Was: JEP draft: Implicit Classes and Enhanced Main Methods (Preview)

Ron Pressler ron.pressler at oracle.com
Tue Feb 21 15:25:06 UTC 2023


I don't see how fields and locals are more easily distinguished by the presence of a class declaration around them — in both cases, things not inside the method belong to an outer scope — nor do I understand why you’d want to emphasise the “instanceness” of an implicit class instance by highlighting `this`. But what I think doesn’t matter; I’m sure that you teach well, and if you don’t want to teach fields before teaching classes — don’t. But I don’t think that your style, however effective, is universal among teachers. Some even avoid teaching OOP in the first term.

I think the criteria for judging this change are:

1. Does it promote a “bad" programming style that would later be discouraged?
2. Does it give teachers significantly more freedom to teach how they choose and addresses the particular problems teachers have complained about?
3. Is it a natural extension of Java?

I think that you agree that the answer to 1 is no and the answers to 2 and 3 is yes, but you point out that some styles of teaching may not be helped by every aspect of the change. I could agree with you completely yet still not see why it matters, unless there’s a way to allow *all* reasonable teaching styles while reducing possible confusion further. I don’t think that disallowing fields achieves that. For one, it makes defining constants very hard. For another, there are valid uses even of mutable shared variables that don’t involve classes. I don’t see how we can help one valid teaching style without harming another or making the feature less natural (e.g. it’s possible that making all members of an implicit class static would make some things easier, but it would harm the gradual-growth goal).

Implicit classes are not meant as a “subset of Java for teaching”. They’re a mechanism that allows teachers to choose what parts of Java they want to teach and when (and allows Java veterans to write small programs more pleasantly). In particular, it allows teachers such as yourself to choose when to introduce fields.

— Ron


On 21 Feb 2023, at 07:09, forax at univ-mlv.fr<mailto:forax at univ-mlv.fr> wrote:



________________________________
From: "Brian Goetz" <brian.goetz at oracle.com<mailto:brian.goetz at oracle.com>>
To: "Remi Forax" <forax at univ-mlv.fr<mailto:forax at univ-mlv.fr>>
Cc: "Ron Pressler" <ron.pressler at oracle.com<mailto:ron.pressler at oracle.com>>, "Dan Heidinga" <heidinga at redhat.com<mailto:heidinga at redhat.com>>, "amber-spec-experts" <amber-spec-experts at openjdk.org<mailto:amber-spec-experts at openjdk.org>>
Sent: Tuesday, February 21, 2023 12:33:31 AM
Subject: Re: Implicit Record Was: JEP draft: Implicit Classes and Enhanced Main Methods (Preview)
You seem to have a very strange notion of what "works" means.  These all work just fine.  No one is suggesting that local and "shared" variables are unified.  If you find it confusing to teach fields early, then wait -- that's one of the choices.

Scope rules are different, being static is different, initialization rules are different, inference rules are different, even colors in IDEs are different.

Correct, and well understood.  Your point?

Implicit class remove the "class" shell, so you now have two syntactically identical things that are semantically different.
IMO, introducing dangling fields, a new feature in Java, does not worth the confusion it creates.



You did, by re-using the term "variables" for both local variable and fields.

Sorry, no.  They are both variables, but they are different sorts.  Just like "instance vs static" variables, or "final vs mutable" variables.  They are all variables (they have a name, and a type, and hold a value), and yet they each have different characteristics (and the characteristics can be combined; you can have shared final static variables, and local mutable variables, and....)

I think you're taking a "I would prefer it work this way" and bootstrapping it into "the alternative is broken" (that's what "doesn't work" means.)  You should know by now that this is the best way to not have your arguments taken seriously!

I think you are talking about variable in general, i'm focused on local variable and fields in the context of implicit class, hence the difficulty to understand each other.
Implicit class makes the syntax of fields identical to the syntax of local variables and at the same time, the way to differentiate then is to explain what an implicit class is.

Again, i'm not against the notion of implicit container, i just think that implicit record makes more sense than implicit class, and i would like we discuss about that,
here is example where the container leaks,

  void main() {
    System.out.println(this);
  }

what it should print ?

regards,
Rémi





On 2/20/2023 4:38 PM, forax at univ-mlv.fr<mailto:forax at univ-mlv.fr> wrote:


________________________________
From: "Brian Goetz" <brian.goetz at oracle.com><mailto:brian.goetz at oracle.com>
To: "Remi Forax" <forax at univ-mlv.fr><mailto:forax at univ-mlv.fr>
Cc: "Ron Pressler" <ron.pressler at oracle.com><mailto:ron.pressler at oracle.com>, "Dan Heidinga" <heidinga at redhat.com><mailto:heidinga at redhat.com>, "amber-spec-experts" <amber-spec-experts at openjdk.org><mailto:amber-spec-experts at openjdk.org>
Sent: Monday, February 20, 2023 9:33:34 PM
Subject: Re: Implicit Record Was: JEP draft: Implicit Classes and Enhanced Main Methods (Preview)


As i said earlier, it does not work because fields and local variables have different semantics,
fields are initialized with a default value while local variables need to be initialized before use.
So the curtain is just a veil that will be pierced by any students moving declarations around.

Of course it "works", it just might not work how you would prefer it to.

  var greetings = 0;

does not work,

  void main() {
    int greetings;
    System.out.println(greetings);
  }

does not work,

  void main() {
    static int greetings;
    System.out.println(greetings);
  }

does not work,

  void main() {
    int greetings = 0;
    for(;;) {
      int greetings = 0;
    }
  }

does not work too.

Scope rules are different, being static is different, initialization rules are different, inference rules are different, even colors in IDEs are different.



Prior to learning about fields, the user can perceive local variables (declared in a method) and "shared" variables (accessible to all members of the class.)  They can learn about their characteristics.  Then, when they learn about classes and fields and accessibility, they can learn that the variables they were calling "shared" are really fields.  The distinction between locals and fields is there from the beginning, though for most use cases, they will not notice the difference.  When they're ready to learn the fine differences, there's not anything to unlearn.

From my personal experience, unifying local variable and field leads to more pain than gain, mostly because local

Who said anything about unification of fields and locals?  Where did you get such an idea that this is what is being proposed?

You did, by re-using the term "variables" for both local variable and fields.



First students will learn about statements.  Then they will probably learn about local variables.  They can be taught that they disappear when the method exits, and each invocation of the method gets a fresh copy.  Then they can learn about multiple methods, and then that there are variables that can be shared across methods and retain their values across method invocations, and while their declaration syntax is the same (they're both variables, after all), the _place_ in which they are declared is different (which is what makes them shared), and shared variables have slightly different characteristics (though not so different they have to learn this immediately).  They can learn the characteristics of shared variables when it makes sense to teach this.  And when the curtain is pulled back, they learn all fields have the characteristics of these shared variables.

Record components behave as you said, they have slightly different characteristics than local variables and it's hard to not notice the difference syntactically.

record Hello(String name) {
  void hello() {
    System.out.println("hello " + name);
  }

  void bonjour() {
    System.out.println("bonjour " + name);
  }
}

Class fields are far more different than just the lifetime but have a very similar syntax.

Rémi

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20230221/26be204b/attachment-0001.htm>


More information about the amber-spec-experts mailing list