[ https://issues.apache.org/jira/browse/GROOVY-10943?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17695186#comment-17695186 ]
ASF GitHub Bot commented on GROOVY-10943: ----------------------------------------- Goooler commented on code in PR #1867: URL: https://github.com/apache/groovy/pull/1867#discussion_r1122002346 ########## src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java: ########## @@ -3906,13 +3906,17 @@ private void validateParameterList(final List<Parameter> parameterList) { for (int n = parameterList.size(), i = n - 1; i >= 0; i -= 1) { Parameter parameter = parameterList.get(i); + String name = parameter.getName(); + if (name.equals("_")) { Review Comment: ```suggestion if ("_".equals(name)) { ``` ########## src/main/java/org/codehaus/groovy/control/PlaceholderVisitor.java: ########## @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.groovy.control; + +import org.codehaus.groovy.ast.ASTNode; +import org.codehaus.groovy.ast.ClassCodeVisitorSupport; +import org.codehaus.groovy.ast.expr.ClosureExpression; +import org.codehaus.groovy.ast.expr.DeclarationExpression; +import org.codehaus.groovy.ast.expr.VariableExpression; + +import java.util.stream.StreamSupport; + +public class PlaceholderVisitor extends ClassCodeVisitorSupport { + public static final String PLACEHOLDER = "Underscore_Placeholder"; + private SourceUnit source; + + public PlaceholderVisitor(CompilationUnit compilationUnit, SourceUnit source) { + this.source = source; + } + + @Override + public void visitDeclarationExpression(DeclarationExpression expression) { + if (expression.isMultipleAssignmentDeclaration() && expression.getTupleExpression().getExpressions().size() > 1) { + long underscoreCount = StreamSupport.stream(expression.getTupleExpression().spliterator(), false) + .map(e -> ((VariableExpression) e).getName()) + .filter(s -> s.equals("_")) + .count(); + if (underscoreCount > 1) { + expression.getTupleExpression().getExpressions().forEach(e -> { + if (((VariableExpression) e).getName().equals("_")) { Review Comment: ```suggestion String name = ((VariableExpression) e).getName(); if ("_".equals(name)) { ``` > Consider additional use of _ as a placeholder > --------------------------------------------- > > Key: GROOVY-10943 > URL: https://issues.apache.org/jira/browse/GROOVY-10943 > Project: Groovy > Issue Type: Improvement > Reporter: Paul King > Priority: Major > Labels: GEP > > Recent Java versions make underscore, "_", an illegal variable name. This is > to allow it to be used as a placeholder as per JEP 302: Lambda Leftovers[1]. > This issue is to explore how better to have such a placeholder in Groovy. > This strengthens an informal convention already in use. Instead of writing > this: > {code} > def (coordX, coordY, unusedZ) = coord3D() > {code} > Sometimes it is written as: > {code} > def (coordX, coordY, _) = coord3D() > {code} > Currently, the underscore is a variable and could be used but the convention > is that > it would be ignored. > This convention doesn't scale if more than one result is to be ignored (here > a double underscore is used for the second ignored result): > {code} > def (coordX, coordY, _, __) = coord3DwithColor() > {code} > This idea being that the following should be valid: > {code} > def (coordX, coordY, _, _) = coord3DwithColor() > {code} > Which currently gives an error for the duplicated variable name. > Another example: > {code} > def (x, _, y, _, z) = 1..5 > assert "$x $y $z" == '1 3 5' > {code} > Similarly, the idea is applicable for lambda parameters (as per the example > in the previously mentioned Java JEP): > {code} > BiFunction<Integer, String, String> biss = (i, _) -> String.valueOf(i) > {code} > We could follow Java's lead and make underscore an invalid variable name but > it might be possible to keep uses of that name except where there is more > than one occurrence of the name. In such a circumstance, use of the name > would no longer reference a variable. We need to check the impacts of > shadowing as per discussions in the JEP. > [1] [https://openjdk.org/jeps/302] > -- This message was sent by Atlassian Jira (v8.20.10#820010)