为什么我不能从方法中显式返回 void?
void run() {
...
if (done) return cancel();
...
}
其中cancel()
返回void
。这不会编译...我几乎可以理解为什么。但如果我想从 void 返回一个 void,为什么不呢?相反,我最终写了这样的内容:
if (done) {
cancel();
return;
}
我不是在寻找代码风格建议,我想知道为什么 Java 明确禁止这种类型的 void 返回。任何信息表示赞赏,谢谢。
void run() {
...
if (done) return cancel();
...
}
where cancel()
return void
. This won't compile... and I can almost understand why. But if I want to return a void from a void, why not? Instead, I end up writing something like this:
if (done) {
cancel();
return;
}
I'm not looking for code style suggestions, I want to know why Java expressly prohibits this type of void return. Any info is appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这是一个有趣的问题。由于 java 强制执行返回类型(
void
是返回类型),因此您的第一个语句似乎是有意义的。我认为这只是为了惯例。由于void
是一个占位符而不是一个对象,因此可能为了语言一致性或编译器简单性而决定将其保留。来自 JLS
更远
It's an interesting question. Since java enforces a return type (
void
is a return type) your first statement seems to make sense. I would take this only for convention. Sincevoid
is a placeholder and not an object, it was probably decided to leave it out for language coherency or compiler simplicity.From JLS
further
带有表达式的 return 语句返回该表达式的值。
cancel()
的类型是一个 void 表达式 - 它没有值。从逻辑上讲,您想要执行
cancel()
,然后返回 - 这就是您必须说的。这两个操作(调用cancel()
然后返回)在逻辑上是不同的。现在,Java可以有一种“unit”类型而不是
void - 但这会影响的不仅仅是返回值。
A return statement with an expression returns the value of that expression. The type of
cancel()
is a void expression - it doesn't have a value.Logically you want to execute
cancel()
, and then return - so that's what you have to say. The two actions (callingcancel()
and then returning) are logically distinct.Now Java could have a sort of "unit" type instead of
void
- but that would affect rather more than just return values.就像这样写:
所以,我认为
void
不是 Java 中的type
。在 C++ 中,return cancel();
是合法的。作为一个熟悉 Java 的 C++ 程序员,答案是:Java 语法不支持很多东西。也许是为了简单或可读性。注意:
void f()
声明类似于 pascal 中的procedure f()
声明,过程不能返回任何值,例如函数,因此我们必须在单独的语句中调用它们。It's like writing:
So, I think
void
is not atype
in Java. In C++,return cancel();
is legal. As a C++ programmer who is familiar with Java the answer is: Many things are not supported in Java syntax. Maybe for simplicity or readibility.Note: A
void f()
declaration is similar to aprocedure f()
declaration in pascal and a procedure could not return any value such as functions, so we must call them in a separated statement.void
不是一种类型。如果您使用Void
< /a> 类型而不是void
关键字,但是,您的代码可以工作,但是:您必须在方法的所有退出点中手动返回 null
。void
is not a type. If you use theVoid
type instead of thevoid
keyword, however, your code will work, but: You'll manually have toreturn null
in all exit points from your method.因为你不会返回
void
。void
不是一个值,因此无法返回。Because you don't return
void
.void
is not a value, so it can't be returned.这是同义反复。意思是,void 定义该方法没有返回值。因此,当 void 根本不返回时,你怎么能“返回 void”呢?
It's a tautology. Meaning, void defines that the method has no return value. Therefore, how can you "return void" when void is no return at all?
简短回答
return cancel()
语句必须返回有效值,但方法声明void run()
声明run()
不返回有效值返回一个值;因此,run()
中的return cancel()
是一个错误。return
语句(不带表达式)尝试将控制权转移给调用者,当方法返回类型为void
时使用;因此,这不是一个错误。长答案
JLS
*return* 语句
部分 指出:JLS
方法返回类型
部分 指出:JLS
类型、值和变量
章第一段指出:JLS
类型和值的种类部分
指出:
现在只需再引述几句。 JLS
表达式语句
部分< /a> 状态:JLS
方法主体
部分指出:最后,JLS
方法声明部分
指出:
现在,当我们将它们拼凑在一起时,我们可以推断出以下结论:
返回
表达式值必须是原始类型或引用类型。void
不是有效的值类型。void
返回类型声明的方法不返回任何值。void run()
不返回值。run()
中,return
(不带表达式)会很乐意将控制权转移给调用者。run()
中,return some expression
是一个错误,因为some expression
必须是有效值,并且run()
> 不返回值。Short Answer
The
return cancel()
statement must return a valid value, but the method declarationvoid run()
declares thatrun()
does not return a value; hence,return cancel()
inrun()
is an error. Thereturn
statement (without an expression) attempts to transfer control to the caller and is used when the method return type isvoid
; hence, not an error.Long Answer
The JLS
The *return* Statement
section states:The JLS
Method Return Type
section states:The JLS
Types, Values, and Variables
chapter, first paragraph states:The JLS
The Kinds of Types and Values
section states:Just a few more quotes now. The JLS
Expression Statements
section states:The JLS
Method Body
section states:And, finally, the JLS
Method Declarations
section states:Now, when we piece it all together, we can deduce the following:
return
statement contains an expression, the expression must evaluate to a valid value.return
expression value must be a primitive type or a reference type.void
is not a valid value type.void
return type, returns no value.void run()
does not return a value.run()
,return
, without an expression, will happily transfer control to the caller.run()
,return some expression
is an error becausesome expression
must be a valid value andrun()
does not return a value.return x
明确表示“返回值 x”,无论该类型是什么(当然,该类型仍然必须与该语句所在的任何函数的返回类型相匹配)。严格来说,
void
是一种类型的不存在,推而广之,就是不存在值 - 因此返回一个值是没有意义的,就像它一样声明void
变量没有意义(也是不允许的)。return x
explicitly means "return the value x", regardless of what that type is (the type, of course, still has to match the return type of whatever function that statement is placed in).void
is, strictly speaking, the absence of a type, and by extension, the absence of a value - so it does not make sense to return one, just like it does not make sense (and is not allowed) to declare avoid
variable.Void 不是真实类型。 Void 只是一个占位符,使方法定义的语法更加一致。这不是java的创新;而是java的创新。这是从 C 继承的。
这就是编译器不允许您编写
return cancel()
的原因,即使方法cancel()
是void
>。Void is not a real type. Void is just a place holder to make syntax of methods definition more consistent. This is not the java innovation; this is inherited from C.
This is the reason that compiler does not allow you to write
return cancel()
even if methodcancel()
isvoid
.void
不是一种类型。方法定义中的void
只是一个占位符,表示不返回任何内容。void
is not a type.void
in the method definition is just a placeholder for returns nothing.有趣的想法。主要问题是语言规范,它将 return 语句定义为由
return
组成。 void 方法不是表达式,因此不允许构造。您发现可以通过执行 void 方法然后返回来复制功能,因此没有真正的理由允许它。
Interesting idea. The main issue is the language spec, which defines a return statement as being composed of
return <expression>
. A void method is not an expression, so the construct isn't permitted.You've found that you can replicate the functionality by executing the void method and then returning, so there's no real reason to allow it.
来自 JLS:
......
From the JLS:
...
Java 语法 实际上并不关心方法调用的类型,所以这不是问题。它必须是在类型检查系统中更下游的东西。我认为最重要的是,如果在 return 关键字之后包含语法上可选的语句,那么系统期望传递一个值。
void
当然是一种类型,但是没有void
类型的值。但是,当然,这些都不能真正解释你问题的答案。正如您所指出的,没有理由不允许使用这种习惯用法。但也没有正当理由允许这样做。所以这是一个折腾。人们可以尝试为他们所做的事情合理化,但这可能毫无意义。
The Java grammar actually doesn't care about the type of a method call, so that's not the issue. It has to be something farther down the chain, in the type-checking system. I think the bottom line is that if a grammatically optional statement is included after the return keyword, then the system expects a value to pass on.
void
certainly is a type, but there are no values with typevoid
.But, of course, none of this really explains the answer to your question. As you pointed out, there's no reason why this idiom should not be allowed. But there's also no valid reason to allow it. So it's a toss up. One could try to rationalize why they did what they did, but that would probably be pointless.
处理这个问题的正确方法是:
A proper way to handle this would be: