JShell错误:“使用特定类名时出现意外类型”

huangapple go评论57阅读模式
英文:

JShell error "unexpected type" when using specific class name

问题

I was just playing around with JShell, and it seems that defining class Z{} and then defining var z = new Z() does not work. But using different class names, like class X and class A, does work.

Surely I must be missing something obvious...?

    |  Welcome to JShell -- Version 14.0.1
    |  For an introduction type: /help intro

    jshell> class X{}
    |  created class X

    jshell> class Z{}
    |  created class Z

    jshell> var x = new X()
    x ==> X@26a1ab54
    |  created variable x : X

    jshell> var z = new Z()
    |  Error:
    |  unexpected type
    |    required: class
    |    found: type parameter Z
    |  var z = new Z();
    |              ^

    jshell> class A{}
    |  created class A

    jshell> var a = new A()
    a ==> A@2ef1e4fa
    |  created variable a : A
英文:

I was just playing around with JShell, and it seems that defining class Z{} and then defining
var z = new Z() does not work. But using different class names, like class X and class A, does work.

Surely I must be missing something obvious...?

|  Welcome to JShell -- Version 14.0.1
|  For an introduction type: /help intro

jshell> class X{}
|  created class X

jshell> class Z{}
|  created class Z

jshell> var x = new X()
x ==> X@26a1ab54
|  created variable x : X

jshell> var z = new Z()
|  Error:
|  unexpected type
|    required: class
|    found:    type parameter Z
|  var z = new Z();
|              ^

jshell> class A{}
|  created class A

jshell> var a = new A()
a ==> A@2ef1e4fa
|  created variable a : A

答案1

得分: 10

使用 var 可能会导致变量具有不可表示的类型。例如,查看表达式的返回类型,该表达式可以是 StringInteger

jshell> /set feedback verbose
jshell> var x = true ? "a" : 1
x ==> "a"
|  created variable x : Serializable&Comparable<? extends Serializable&Comparable<?>&java.lang.constant.Constable&java.lang.constant.ConstantDesc>&java.lang.constant.Constable&java.lang.constant.ConstantDesc

jshell 评估您的代码片段时,如果出现这种情况,它会将其封装在一段代码块中以便稍后记录此类型的使用。封装的片段包括一个名为 Z 的通用类型参数:

        // private static <Z> Z do_itAux() {
        //     wtype x_ = y;
        //     @SuppressWarnings("unchecked")
        //     Z x__ = (Z) x_;
        //     return x__;

这个参数的名称会泄漏到正在评估的代码块中,这意味着类名会被类型参数遮蔽。这使得 Z 成为一个特例 -- 其他单个字符的例子是没问题的。

英文:

Use of var can lead to a variable having a non-denotable type. For example, looking at the return type of an expression that could be String or Integer:

jshell> /set feedback verbose
jshell> var x = true ? "a" : 1
x ==> "a"
|  created variable x : Serializable&Comparable<? extends Serializable&Comparable<?>&java.lang.constant.Constable&java.lang.constant.ConstantDesc>&java.lang.constant.Constable&java.lang.constant.ConstantDesc

When jshell is evaluating your code fragment, if this is the case, it wraps it in a block of code so that it can record this type for later use. The wrapping fragment includes a generic type parameter called Z:

        // private static <Z> Z do_itAux() {
        //     wtype x_ = y;
        //     @SuppressWarnings("unchecked")
        //     Z x__ = (Z) x_;
        //     return x__;

This parameter's name leaks into the code block being evaluated, meaning the class's name is shadowed by the type parameter. This makes Z a special case -- the other single character examples are fine.

huangapple
  • 本文由 发表于 2020年5月30日 20:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/62102369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定