发现 Kotlin Vertx 类型不匹配 Future<Unit> 预期 Handler<AsyncResult<Void>>

Kotlin Vertx Type Mismatch found Future<Unit> expected Handler<AsyncResult<Void>>

我认为在 Kotlin 中,Unit 等同于 Void。使用 Vert.x 服务发现,不可能将 Future<Unit> 传递给 unpublish(String id, Handler<AsyncResult<Void>> resultHandler)(导致类型不匹配),但它会毫无问题地接受 Future<Void>。为什么会这样,是否有解决方案,或者我只能忍受使用 Void?

Unit不等同于Void,它等同于kotlin中的void

在java中,void是关键字,而Void是class。所以下面的代码无法编译:

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.