JEP draft: Scope Locals
Cay Horstmann
cay.horstmann at gmail.com
Fri May 14 09:03:09 UTC 2021
On 14/05/2021 10:31, Andrew Haley wrote:
> On 5/13/21 6:28 PM, Douglas Surber wrote:
>> In most of you examples the ScopeLocal could easily be replaced with a local variable.
>
>> The CREDENTIALS example though does show the value of ScopeLocals, that CREDENTIALS can be dereferenced in the method connectDatabase() which is outside the lexical scope where creds is defined. It would help if the first example was along this line and clearly called out how a ScopeLocal (really a DynamicScopeLocal) is different from a lexically scoped local variable.
>
> I see. OK.
>
I had exactly the same reaction. When I saw
// Declare scope locals x and y
static final ScopeLocal<MyType> x = ScopeLocal.forType(MyType.class);
static final ScopeLocal<MyType> y = ScopeLocal.forType(MyType.class);
{
ScopeLocal.where(x, expr1)
.where(y, expr2)
.run(() -> ... code that uses x.get() and y.get() ...);
}
I mentally refactored into
{
MyType x = expr1;
MyType y = expr2;
run(() -> ... code that uses x and y ...);
}
and thought "what's the point???" I had to remind myself that "code that
uses x and y" means "code that calls other code that uses x and y and
wants them to be populated with the values for this run".
Why not start out with the CREDENTIALS example? It's nice and concrete.
Cheers,
Cay
--
Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com
More information about the loom-dev
mailing list