Draft JLS spec for records - local types

Brian Goetz brian.goetz at oracle.com
Sun Nov 10 15:38:53 UTC 2019


Sorry, I know I’ve said this before, but I guess not on this thread.  Local records are always implicitly static — they cannot capture anything.  (Local enums would be the same way.)  As you point out, the hidden state would undermine the “the record description is the state, the whole state, and nothing but the state” directive.  

> On Nov 9, 2019, at 4:31 PM, Tagir Valeev <amaembo at gmail.com> wrote:
> 
> Hello!
> 
>> Local records should be allowed; we should probably allow local enums at the same time.
> 
> On the other hand, local classes also may have an implicit state which
> consists of captured variables and possibly captured `this` reference
> as well. Usually, all instances of local classes that can interact
> with each other have the same captured state, but it's not impossible
> to have two instances with the different captured state. E.g.:
> 
> public static void main(String[] args) {
>   foo(0);
> }
> 
> static Object foo(int x) {
>   class Foo {
>      int getX() { return x; }
>   }
>   if (x == 1) {
>      return new Foo();
>   }
>   Foo foo1 = new Foo();
>   Foo foo2 = (Foo) foo(1);
>   System.out.println(foo1.getX()); // prints 0
>   System.out.println(foo2.getX()); // prints 1
>   return null;
> }
> 
> If we allow local records, they would also have an implicit state,
> that "hidden extra component". E.g. if we define a record without
> components instead of class Foo in the example above, its `equals`
> method would return true for `foo1` and `foo2`, yet they have
> different behavior. Is it still ok?
> 
> With best regards,
> Tagir Valeev.



More information about the amber-spec-experts mailing list