1

I've got some generic processors that get invoked by a framework. Argument is the payload that I process.

interface Processor<T, U> {
    U process(T t);
}

There are situations in which payload makes no sense and does not exist (only response is calculated and returned). In that case the framework passes null as argument. I decided to use javax.lang.model.type.NullType for that purpose:

new Processor<NullType, String>() {
  public String process(NullType unused) {
    ...
    return result;
  }
}

Am I misusing the intention of NullType here? Is java.lang.Void or something else more appropriate?

Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • *" This is the type of the expression null"* ... so it represents that you want it to represent ... where is the confusion, now? – Tom Apr 08 '16 at 11:31
  • @Tom well, potentially that two instances of `NullType` aren't actually equal (as it is an interface) - so they represent `null`, but `Objects.equals(aNullType, bNullType) == false`, whereas `Objects.equals(null, null) == true`. – Andy Turner Apr 08 '16 at 11:32
  • When working with the android sdk, `Void` is the commonly used type in these cases. – SME_Dev Apr 08 '16 at 11:52

1 Answers1

4

I prefer Void in these situations, which makes passing a null the only valid parameter.

NullType on the other hand can be instantiated.


As a curiosity, it is possible to instantiate an object of type Void.

Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I agree - here's why: NullType is meant to represent a type. In the same way that ArrayType (in the same package) is not an array, NullType is not null. It's normal to use Void in similar cases, like Callable or Future – Stephen Rosenthal Apr 08 '16 at 11:40
  • I've used `Void` also in similar situations so far, but I always had a feeling that it's meant to deal with return types. – Dragan Bozanovic Apr 08 '16 at 11:50