相关疑难解决方法(0)

Pattern Matching for instanceof throwing a confusing error: expression type Triple is a subtype of pattern type Triple

I have Java 19, and I am attempting to do some simple pattern-matching on a record that I created. However, Java is giving me a very confusing compilation error. Here is the simplest example I could make that causes the error.

public class ExpressionTypeIsASubsetOfPatternType
{

   public record Triple(int a, int b, int c) {}

   public static void main(String[] args)
   {

      System.out.println("Java Version = " + System.getProperty("java.version"));

      final Triple input = new Triple(1, 2, 3);

      if (input instanceof Triple t)
      { …
Run Code Online (Sandbox Code Playgroud)

java instanceof pattern-matching java-record

3
推荐指数
1
解决办法
1582
查看次数

标签 统计

instanceof ×1

java ×1

java-record ×1

pattern-matching ×1