[External] : Re: Scope Locals

Jack Firth jackhfirth at gmail.com
Tue Apr 27 20:47:21 UTC 2021


No, what I meant was literally that I think the multi-variable case looks
weird if the static entrypoint method for starting a chain of bindings also
accepts a binding. I would rather see something like this:

ScopeLocal.builder()
    .with(x, expr1)
    .with(y, expr2)
    .run(() -> ...);

When I used a hypothetical scope() method earlier, I was imagining it would
be statically imported.

For context, I work at Google and we use google-java-format
<https://github.com/google/google-java-format> for formatting code. One of
the lessons we've learned is that trying to keep stuff horizontally aligned
interacts badly with refactoring tools and autoformatters. Even if the
tools can preserve it correctly, it means that things like renaming a
method can have much larger diffs than they otherwise would. If ScopeLocal
was renamed to ScopeBuilder, for example, only the ScopeLocal.builder()
line would change in my example above. But if the code was written as:

ScopeLocal.with(x, expr1)
           .with(y, expr2)
           .run(() -> ...);

...then renaming ScopeLocal to ScopeBuilder would require that the whole
chain be reindented.

For the one-variable case, I'd probably just prefer an instance method on
the variables themselves:

x.runWith(expr, () -> ...);


On Tue, Apr 27, 2021 at 11:03 AM Brian Goetz <brian.goetz at oracle.com> wrote:

> I think what Jack is getting at is that the case where there is only one
> scope variable will be common, and is worth optimizing both the expression
> and implementation of this.  So something like:
>
>     interface ScopeLocal<T> {
>         void runWith(T t, Runnable r);
>
>         static<T> ScopeLocalBuilder with(ScopeLocal<T> sl, T expr) {
>             return new ScopeLocalBuilder().with(sl, expr);
>         }
>     }
>
>     class ScopeLocalBuilder {
>         ScopeLocalBuilder() { }
>
>         <T> ScopeLocalBuilder with(ScopeLocal<T> sl, T expr) { ... }
>
>         void run(Runnable r);
>     }
>
> This admits the easy (and possibly more optimizable) locution
>
>     x.runWith(expr1, r)
>
> for the one-variable case, and
>
>     ScopeLocal.with(x, expr1)
>               .with(y, expr2)
>               .run(r)
>
> for the fancy case.
>
> On 4/27/2021 11:59 AM, Andrew Haley wrote:
>
> On 4/20/21 2:20 AM, Jack Firth wrote:
>
> It seems strange that where() is both a static method on ScopeLocal and an
> instance method on the carrier. Maybe have a separate parameter-less static
> method for starting the chain?
>
> scope()
>     .where(x, expr1)
>     .where(y, expr2)
>     .run(() -> ...);
>
> The method scope() still has to be a member of some class, and you'd
> have to specify that somehow. Whatever we do will be slightly strange,
> I think.
>
>
>
>


More information about the loom-dev mailing list