Home > Software design >  Use Function as Supplier
Use Function as Supplier

Time:01-26

Because of inheritance, a leaf-class has to have a Function<T, R> which it uses as a Supplier<R>. (Ignore the passed parameter)

What is the best way to convey that the generic parameter type is not used and that null should get passed?

CodePudding user response:

To convey the notion that some variable, return type, parameter etc. can only be null the type Void can be used - there's only one valid value for this: null.

So in your case you could use Function<Void, R> to indicate apply(null) will always be called, so the function needs to be mapped to a supplier of sorts. In general you'd want to use Supplier<R> instead but in cases where Function is required this could be an option.

The same would be true for consumers that would need to be represented as functions: use Function<T, Void> in those cases and return null.

CodePudding user response:

Use java.lang.Void

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

It can't be instantiated, so only null can be passed as parameter.

class SomeClass implement Function<Void, YourResultClass> {
  //implement apply
}
  • Related