Scope Locals

Jack Firth jackhfirth at gmail.com
Tue Apr 20 01:20:17 UTC 2021


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(() -> ...);

On Mon, Apr 19, 2021 at 5:49 AM Andrew Haley <aph at redhat.com> wrote:

> On 4/3/21 3:27 PM, Brian Goetz wrote:
>
> > Andrew Haley wrote:
> >> I've been kicking around various ideas and implementations of scope
> >> locals for some time, and I've now got something to share with the
> >> wider world
> >> ...
> >>    // Declare to scope locals x and y
> >>    static final ScopeLocal<MyType> x = ... ;
> >>    static final ScopeLocal<MyType> y = ... ;
> >>
> >>    {
> >>      ScopeLocal.set(x, expr1)
> >>                .set(y, expr2)
> >>                .in(() -> ... code that uses x and y ...)
> >>    }
> >
> > This looks like a good API direction for this.  It scales to multiple
> > SLs, and makes it exceedingly clear what their scope is.  I assume that
> > if you're only setting one, you might also have a simpler form for
> >
> >      ScopeLocal.with(x, expr, () -> code)
> >
> > that doesn't produce an intermediate carrier.
>
> OK.
>
> > Yes, this offers some flexibility.  Some obvious questions, which
> > probably have obvious answers:
> >
> >   - Presumably the set() method doesn't actually set anything, but
> > instead sets up a pair which will be bound before calling the in()
> >   - Presumably the result of SL.set() is a "carrier" that encapsulates a
> > chain of bindings
> >   - Presumably the carrier can be reused:
> >
> >      ScopeLocalCarrier sc = ScopeLocal.set(x, e1).set(y, e2);
> >      sc.in(x);
> >      sc.in(y);
>
> Yes, yes, and yes.  :-)
>
> >> I'm not entirely sure about the naming here. set() is given a
> >> different meaning from usual, and I'm not sure anyone would let me get
> >> away with using let(). in() is slightly odd too, and perhaps something
> >> more verbose would be better.
> >>
> >> Other possibilities are e.g.
> >>
> >>      ScopeLocal.bind(x, expr1)
> >>                .bind(y, expr2)
> >>                .exec(() -> ... code that uses x and y ...)
> >
> > This naming is a definite improvement over the version above, for the
> > reasons you cite.  The let ... in might have been the mental model that
> > kicked off this line of thinking, but the terminology may not come along
> > for the ride.  Perhaps `withBinding(sl, expr)`, which evokes a little
> > bit of "save this for later" or "use this in that context."
>
> For the initial JEP I'm going with John Rose's "where", so it reads like:
>
>      ScopeLocal.where(x, expr1)
>                .where(y, expr2)
>                .run(() -> ... code that uses x and y ...)
>
> Thanks,
>
> --
> Andrew Haley  (he/him)
> Java Platform Lead Engineer
> Red Hat UK Ltd. <https://www.redhat.com>
> https://keybase.io/andrewhaley
> EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
>
>


More information about the loom-dev mailing list