`with` across function calls

JN 666total at wp.pl
Fri Jan 18 14:23:48 UTC 2019


On Friday, 18 January 2019 at 12:35:33 UTC, Nicholas Wilson wrote:
> void usercode()
> {
>      {
>          auto g = ...;
>          with (g) // or with g:
>          {
>              bar(); // calls: bar(g);
>          }
>      }
>      // or
>      {
>          with g = ...;
>          Baz.baz(); // calls: Baz.baz(g);
>      }
> }
>
> Obviously this requires a DIP, but what you do think of it?

Not a big fan of such implicit stuff. I think it'd be very hard 
to debug if something goes wrong.

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.

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


More information about the Digitalmars-d mailing list