`with` across function calls

Nicholas Wilson iamthewilsonator at hotmail.com
Fri Jan 18 14:40:40 UTC 2019


On Friday, 18 January 2019 at 14:23:48 UTC, JN wrote:
> Not a big fan of such implicit stuff. I think it'd be very hard 
> to debug if something goes wrong.

Thats what a debugger is for, it would behave just like any other 
parameter. The debug workflow wouldn't really change, not anymore 
than of using default parameters.

Think of it like a context aware default parameter.

> Your usecase reminds me a bit of Dependency Injection in OOP 
> world, or even Service Locator pattern. With DI frameworks, 
> you'd declare a function/class to require a "g", but you don't 
> have to pass it to every call. Instead, you obtain your "g" 
> from a global provider class.

I'm trying to avoid globals and this is a structs wrapping opaque 
handles to OOP objects code, I don't deal in classes at all.

> Is 'with' commonly used in D? I don't think I've ever seen it 
> used in any sourcebase. I think it's main usecase is to 
> initialize structs. I kind of like the .. pattern that Dart has:
>
> Foo f = new Foo()
> ..x = 10     // equivalent to f.x = 10
> ..y = 20    // equivalent to f.y = 20

It used pretty frequently (for some value of frequently) for 
switching on enums to avoid retyping the prefix.

mir-glas uses it for register-blocking
https://github.com/libmir/mir-glas/blob/fd9adb0750c23db4c3948f79b63384b8082f3601/source/glas/internal/symm.d#L173
and more general for transparent access to config-type structs 
that need to be short lived (i.e. have a dtor that needs running).


More information about the Digitalmars-d mailing list