Hi Hector, all,

I think of the Lookup API as a great tool to extend the capabilities of classes _without_ using inheritance.

Once your classes implement the "Lookup.Provider" interface you can return Elephant.class, CupOfTea.class and even Beer.class implementations from your getLookup() _without_ altering your API.

This is the biggest problem I see with the PR: you want to add a new feature to the Lookup.class (the Optional support) using inheritance, and not using the Lookup API itself. That's weird for me.

You could always create a NetBeans module with an Optional-aware (Stream-aware too?) "LookupOptional" interface in the API, a simple implementation and then register the interface in the META-INF/services (@Service) of the module as we always do.

Once a dependency on this module is ready one could write:

----
Lookup lookup = ...

LookupOptional lookupOptional = lookup.lookup(LookupOptional.class);

Whatever w = lookupOptional.lookup(lookup, Whatever.class).orElse(...)

----

If creating a new module for this Optional (Streamy?) features is too much of an effort then, as an alternative, I'd suggest adding a new method to org.openide.util.lookup.Lookups like so:

----
public static <T> Optional<T> of(Lookup lookup, Class<T> clazz) {
        return Optional.ofNullable(lookup.lookup(clazz));
}
----

So one could write:

----
Lookup lookup = ...
Whatever x = Lookups.of(lookup, Whatever.class).orElse(...)
----

I think that the first approach is cleaner as it does not touch the Lookup API at all, and keeps backwards compatibility.

Kind regards,
Antonio

P.S.: As a curiosity: I'm afraid I'm on the Optional-dislikers group, so if you finally propose a "LookupOptional" interface in a new module I'm afraid I won't be using it myself.


[1]
https://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/Lookups.html





El 2/7/20 a las 18:32, Hector Espert escribió:
A few months ago, I created a draft PR to add a method in the Lookup class
that returns a Java optional.

https://github.com/apache/netbeans/pull/2063

There wasn't any special reason. Only I used it to study how the Netbeans
documentation works.

If anybody is interested in that, I will continue with the development. If
not, I will close the PR.

Regards, Hector


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists



Reply via email to