Search code examples
kotlinvert.xkotlin-interop

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


I thought that in Kotlin, Unit was equivalent to Void. With Vert.x Service Discovery, it is not possible to pass a Future<Unit> to unpublish(String id, Handler<AsyncResult<Void>> resultHandler) (gives a type mismatch) yet it will accept Future<Void> without any problem. Why is this and is there a solution or will I just have to live with using Void?


Solution

  • Unit is not equivalent to Void, it is equivalent to void in kotlin.

    In java, void is a keyword, but Void is a class. so the code below can't be compiled:

    fun foo():Void{/**need return a Void instance exactly**/}
    
    fun bar():Void{ return Unit; }
    //                     ^--- type mismatch error
    

    java applies the same rule, for example:

    Void canNotBeCompiled(){
      // must return a Void instance exactly.
    }
    
    Void foo(){
      return Void.TYPE;
    }
    
    Void nil(){
      return null;
    }
    

    Finally the Unit documentation also says:

    The type with only one value: the Unit object. This type corresponds to the void type in Java.