Skip to content

java17模式匹配下if语句体编译报错 #13

@qw623577789

Description

@qw623577789
    @Async
    private JPromise<Void> simpleAwait() {
        Object obj = null;
        
        // 这种写法没问题
        if (obj instanceof String) {
            String stringObj = (String) obj;
            JAsync.just(null).await();
            System.out.println(stringObj);
        }

        // 这种写法编译报错
        if (obj instanceof String stringObj) {
            JAsync.just(null).await();
            System.out.println(stringObj);
        }
        return JAsync.just(null);
    }

    public static void main(String[] args) {
        App app = new App();
        System.out.println(app.simpleAwait().block());
    }


System.out.println(stringObj);
^
符号: 变量 stringObj
位置: 类 App

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions