Suppliers class should provide a valueOf(Supplier) method
See original GitHub issueSuppliers class has an ofInstance(T instance) method which accept a object of type T as value and return a supplier that provides that value. It would be better off that it also has a method for the opposite direction: given a supplier, return it’s value. Although we can easily get the value of a supplier by calling supplier.get(), it is feasible only when we already defined a supplier explicitly, we can not do it with a Lambda expression. For example:
String str = (() -> { return "abc" + "def"; }).get();
The code above is invalid. We have to write this:
Supplier<String> supplier = () -> {return "abc" + "def"; };
String str = supplier.get();
But if Suppliers class can have a valueOf method, we can write this:
String str = valueOf(() -> {return "abc" + "def"; });
That would be much more elegant. Of course, with a very short amount of inner code, it’s not necessary to put code into lambda, but if there are many lines of code, putting it into a Lambda will give us better code separation of concern, hence easier to understand. Besides, providing such a method does no harm to the class.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top GitHub Comments
Wouldn’t you then just typically put the code in a separate private method and just call it?
You can just cast it into one supplier.