On 17/05/2023 02:33, John Hendrikx wrote:
SymbolLookup lookUp = name -> library1.find(name)
           .or(() -> library2.find(name))
           .or(() -> loader.find(name));

What you say is true - e.g. the fact that a lookup returns an optional can be used to chain multiple lookups as you describe.

There are, however some difference - note that your example only uses "captures" symbol lookup variable names - so the full code would be more something like:

```java
SymbolLookup library1 = SymbolLookup.libraryLookup("lib1", arena);
SymbolLookup library2 = SymbolLookup.libraryLookup("lib2", arena);
SymbolLookup loader = SymbolLookup.loaderLookup();
SymbolLookup lookUp = name -> library1.find(name)
          .or(() -> library2.find(name))
          .or(() -> loader.find(name));
```

Without the initial setup, we could try to write something like this:

```java
SymbolLookup lookUp = name -> SymbolLookup.libraryLookup("lib1", arena).find(name)
          .or(() -> SymbolLookup.libraryLookup("lib2", arena).find(name))
          .or(() -> SymbolLookup.loaderLookup().find(name));
```

But this more compact version has several issues. First, it creates a new library lookup on each call to "find" (possibly more). Second, the loader in which the lookup is retrieved depends on who calls the "find" method.

So, the more compact version is not "like for like" for the more verbose option above. And this is due to the fact that, in most cases, retrieving a symbol lookup has some side-effects, such as loading a library - it is *not* a purely functional process.

With the new method, it becomes like this:

```java
SymbolLookup lookUp =SymbolLookup.libraryLookup("lib1", arena)
          .or(SymbolLookup.libraryLookup("lib2", arena))
          .or(SymbolLookup.loaderLookup());
```

So, not only the new method results in more succinct code (compared to the alternatives), but it also combines symbol lookups in the "right way", so that the chain of lookups is defined, once and for all, when the composite lookup is first created.

Cheers
Maurizio

Reply via email to