我以为在Kotlin中,Unit
等于Void
。使用Vert.x Service Discovery,无法将Future<Unit>
传递给unpublish(String id, Handler<AsyncResult<Void>> resultHandler)
(给出类型不匹配),但是它将毫无问题地接受Future<Void>
。为什么会这样,有解决方案吗?还是我只需要使用Void
?
最佳答案
Unit
不等于Void
,等效于kotlin中的void
。
在Java中,void
是一个关键字,但是Void
是一个类。因此以下代码无法编译:
fun foo():Void{/**need return a Void instance exactly**/}
fun bar():Void{ return Unit; }
// ^--- type mismatch error
Java应用相同的规则,例如:
Void canNotBeCompiled(){
// must return a Void instance exactly.
}
Void foo(){
return Void.TYPE;
}
Void nil(){
return null;
}
最后,Unit文档还说:
The type with only one value: the Unit object. This type corresponds to the void type in Java.
关于kotlin - 找到Kotlin Vertx类型不匹配Future <Unit>预期的Handler <AsyncResult <Void >>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44773682/