英文:
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
可能会导致变量具有不可表示的类型。例如,查看表达式的返回类型,该表达式可以是 String
或 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
当 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论