Having to delete a sealed interface and start from scratch to add additional implementers, feels unintuitive
Lingo Coder
plugins at lingocoder.com
Sat Aug 8 09:07:05 UTC 2020
My Environment:
------------------------------------------------------------
uname -a
Linux lingocoder 4.15.0-76-generic #86-Ubuntu SMP Fri Jan 17
17:24:28 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
java -version
openjdk version "15" 2020-09-15
OpenJDK Runtime Environment (build 15+35-1559)
OpenJDK 64-Bit Server VM (build 15+35-1559, mixed mode, sharing)
------------------------------------------------------------
But the same thing happens on Windows amd64 with OpenJDK Runtime
Environment (build 15-ea+31-1502)
-------- Original Message --------
Subject: Having to delete a sealed interface and start from scratch to
add additional implementers, feels unintuitive
From: "Lingo Coder" <plugins at lingocoder.com>
Date: Fri, August 07, 2020 4:14 pm
To: "kulla-dev at openjdk.java.net" <kulla-dev at openjdk.java.net>
I apologize if this is the wrong place to discuss a Sealed
Interface/Records
issue in JShell.
The issue is: The only way to update a previously-created sealed
interface
in JShell, to add new/forgotten records to its permits list, is to first
/drop the original interface, then declare the additional permitted
record(s),
then recreate the sealed interface from scratch; including the
additional
implementing record(s) in the permits clause of the recreated sealed
interface.
Steps to Reproduce
1. Launch jshell --enable-preview
2. Create a sealed interface
jshell> sealed interface Animal permits Cat, Dog { String breed(); }
| created interface Animal, however, it cannot be referenced until
class Cat,
and class Dog are declared
3. Create the two records that you just declared to implement the sealed
interface:
jshell> record Cat(String breed, String name) implements Animal{}
jshell> record Dog(String breed, String name, boolean bites) implements
Animal{}
The interface and two records now exist:
jshell> /types
| interface Animal
| record Cat
| record Dog
5. Now /edit the interface to permit a third record:
/edit Animal
sealed interface Animal permits Cat, Dog, Fish { String breed(); }
| Error:
| cannot find symbol
| symbol: class Fish
| sealed interface Animal permits Cat, Dog, Fish { String breed();}
| ^--^
| Error:
| invalid permits clause
| (illegal reference to supertype Fish)
| sealed interface Animal permits Cat, Dog, Fish { String breed();}
| ^-^
4. No luck adding the third record either:
jshell> record Fish(String breed, String color) implements Animal{}
| Error:
| class is not allowed to extend sealed class: Animal
| record Fish(String breed, String color) implements Animal{}
| ^---------------------------------------------------------^
It seems like I should be able to update the interface by simply
/editing its declaration. It feels awkward and unintuitive to be
unable to add additional implementations by /editing the permits
in the interface's declaration.
Are there plans to add that capability to JShell at any point in the
future?
Also, after several attempts at /editing the interface kept erroring,
I observed that JShell got in such a state that even after deleting
the interface to start over, adding the new record or recreating the
interface crashed JShell.
Until I can pin down a more reproducible pattern though, I'll assume
that's more likely down to me having done something wrong.
TIA.
More information about the kulla-dev
mailing list