Java generics void/Void types

后端 未结 6 1314
天命终不由人
天命终不由人 2020-12-08 09:36

I am implementing a ResponseHandler for the apache HttpClient package, like so:

new ResponseHandler() {
    public int handleResponse         


        
6条回答
  •  無奈伤痛
    2020-12-08 10:00

    Alas it's not possible. You can set the code to return Void as you say, however you can never instanciate a Void so you can't actually write a function which conforms to this specification.

    Think of it as: The generic function says "this function returns something, of type X", and you can specify X but you can't change the sentence to "this function returns nothing". (I'm not sure if I agree with these semantics, but that's the way they are.)

    In this case, what I always do, is just make the function return type Object, and in fact always return null.

提交回复
热议问题