[External] : Re: JEP draft: Implicit Classes and Enhanced Main Methods (Preview)

forax at univ-mlv.fr forax at univ-mlv.fr
Thu Feb 16 21:41:13 UTC 2023


> From: "Ron Pressler" <ron.pressler at oracle.com>
> To: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "amber-spec-experts" <amber-spec-experts at openjdk.org>
> Sent: Thursday, February 16, 2023 12:52:24 AM
> Subject: Re: [External] : Re: JEP draft: Implicit Classes and Enhanced Main
> Methods (Preview)

>> On 15 Feb 2023, at 18:18, Remi Forax < [ mailto:forax at univ-mlv.fr |
>> forax at univ-mlv.fr ] > wrote:

>> What is the behaviors of the compiler for

>> var hello() { }
>> var hello() { return; }
>> var hello() { return null; }
>> var foo;
>> var foo = null;

>> record hello() { }
>> record hello() { return; }
>> record hello() { return null; }
>> record foo;
>> record foo = null;

>> record main() { }
>> record main() { return; }
>> static record main() { }
>> static record main() { return; }
>> record main;
>> record main = null;

> All of these except for `record hello() {}`, `record main() {}`, and `static
> record main() {}` are compile-time errors.

> An implicit class is compiled as if the entire content of the file (except
> import statements) are enclosed in a final class declaration with some unknown
> name. Because there is no name, you can’t declare a constructor (or use a
> method reference to a static method). The meaning of all members is interpreted
> in the usual way.

or refers to "this" by the name of the class. 

My questions was more about the error messages students will get when writing one of these lines. 
Usually students struggle not because they have make a mistake but because they are not able to understand how to fix it. 

> The only special rules are that an implicit class must be in the unnamed package
> and it must contain a main entry point (if it didn’t have an entry point there
> would be no way of using the class — you can’t invoke a static method on it
> from some other class or instantiate it because it’s unnamed).

I still think that fields should not be allowed inside an implicit class, because when you remove the class declaration a field and a local variable are too similar and because an implicit class has no user defined constructor. 

Here is a series of examples showing how confusing it can be. 

Object var, record; 
void main() { 
System.out.println(var); // compiles 
} 
void main() { 
Object var, record; 
System.out.println(var); // does not compile 
} 
Object var, record; 
static void main() { 
System.out.println(var); // does not compile 
} 

static Object var, record; 
void main() { 
System.out.println(var); // compiles but awful 
} 

void main() { 
var var = 3; 
System.out.println(var); // compiles 
} 

var var = 3; // does not compile 
void main() { 
System.out.println(var); 
} 

Also conceptually, being able to define fields without constructors is problematic, because you are bypassing the the notion of encapsulation. 
Implicit class instance fields are more complex that usual class fields because of the lack of constructors. 

Teaching using a simpler model is great but not if as a student you have to unlearn something previously introduced. 

> — Ron

Rémi 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20230216/8bd58ba3/attachment.htm>


More information about the amber-spec-experts mailing list