You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Oscar N (Jira)" <ji...@apache.org> on 2025/04/21 12:12:00 UTC

[jira] [Created] (GROOVY-11628) Support record deconstruction inside method parameter

Oscar N created GROOVY-11628:
--------------------------------

             Summary: Support record deconstruction inside method parameter
                 Key: GROOVY-11628
                 URL: https://issues.apache.org/jira/browse/GROOVY-11628
             Project: Groovy
          Issue Type: Bug
          Components: syntax
            Reporter: Oscar N


Java supports record deconstruction in switch and instanceof, like this:

{code:java}
record BlockPos(int x, int y, int z) {}
record BlockBreakEvent(String username, BlockPos pos) {}
	
public static void main(String[] args) {
    var event = new BlockBreakEvent("Bob", new BlockPos(50, 100, 300));

    switch (event) {
        case BlockBreakEvent(String username, BlockPos(int x, int y, int z)) -> 
            LOGGER.info("Player {} broke a block at ({}, {}, {})", username, x, y, z);
    }

    if (event instanceof BlockBreakEvent(String username, BlockPos(int x, int y, int z)))
        LOGGER.info("Player {} broke a block at ({}, {}, {})", username, x, y, z);
}
{code}

However, this cannot be used directly in method parameters, making the syntax sugar less useful for code styles that involve the publisher/subscriber pattern with a subscriber method per event type. I'd like to request support for this in Groovy:

{code:groovy}
static void onBlockBreak(BlockBreakEvent(String username, BlockPos(int x, int y, int z)) {
    log.info "Player $username broke a block at ($x, $y, $z)"
}
{code}

The above would be equivalent to the following code:
{code:groovy}
static void onBlockBreak(BlockBreakEvent event) {
    String username = event.username()
    def (int x, int y, int z) = event.pos()
    log.info "Player $username broke a block at ($x, $y, $z)"
}
{code}

Relates to https://issues.apache.org/jira/browse/GROOVY-11589



--
This message was sent by Atlassian Jira
(v8.20.10#820010)