asvanberg opened a new pull request, #684: URL: https://github.com/apache/wicket/pull/684
Wicket currently leaves a lot to be desired when working with polymorphic datastructures. Consider the following scenario: ```java interface TextMatchingService { sealed interface Status { record NotSubmitted(Runnable submit) implements Status {} record Queued(Instant since, Optional<Instant> expectedFinish) implements Status {} record Analysed(float percentMatch) implements Status {} record Error(int errorCode, String humanReadableMessage) implements Status {} } Status getStatus(File file); } ``` Now I want to have a panel that gets the status of a file and then, depending on the status, shows the different cases. If I were to unpack the model and `switch` over the type in the constructor, when the value returned by the `IModel<Status>` changes due to the asynchronous nature of the text matching the hierarchy would be out of sync. The proposed `poly` function would enable the following pattern for dealing with this case. ```java public class TextMatchingPanel extends GenericPanel<File> { @Inject TextMatchingService textMatchingService; public TextMatchingPanel(String id, IModel<File> fileModel) { super(id, fileModel); IModel<Status> statusModel = fileModel.map(textMatchingService::getStatus); add(new NotSubmittedPanel("not_submitted", statusModel.poly(NotSubmitted.class)); [add more specific status panels] } class NotSubmittedPanel extends Panel { NotSubmittedPanel(String id, IModel<NotSubmitted> notSubmittedModel) { super(id, notSubmittedModel); add(new Link<>("submit") { @Override public void onClick() { NotSubmitted notSubmitted = notSubmittedModel.getObject(); Runnable submitForTextMatching = notSubmitted.submit(); submitForTextMatching.run(); } }); } @Override protected void onConfigure() { super.onConfigure(); setVisible(getDefaultModelObject() != null); } } [more status panels] } ``` If there's a better way to do this, preferably using switch expressions (to get totality checking), that would be preferred but I've yet to come up with one. Unpacking in the constructor and creating different component hierarchies would work if the page was stateless but then there would also be no need for a detachable `IModel` since nothing gets serialized. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@wicket.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org