www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23503] New: Add `lazy with`

https://issues.dlang.org/show_bug.cgi?id=23503

          Issue ID: 23503
           Summary: Add `lazy with`
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

I suggest an alternative version of `with (typeOrExpr)` that resolves an
identifier `id` to `typeOrExpr.id` only if `id` cannot be resolved otherwise.

Its syntax can be `lazy with (typeOrExpr)`.

Example:

```d
enum A { x, y    }
enum B {    y, z }

void main()
{
    with (A)
    with (B)
    {
        pragma(msg, typeof(x)); // prints: A
        pragma(msg, typeof(y)); // prints: B //!
        pragma(msg, typeof(z)); // prints: B
    }

    lazy with (A)
    lazy with (B)
    {
        pragma(msg, typeof(x)); // prints: A
        pragma(msg, typeof(y)); // prints: A //!
        pragma(msg, typeof(z)); // prints: B
    }
}
```

When resolving `y`, in the first case, `with (B)` goes ahead and resolves it
(greedily) and `with (A)` has nothing left to do.

In the second case, `lazy with (B)` asks its surrounding context to resolve `y`
and `lazy with (A)` then does the same. The answer for `lazy with (A)` is that
no `y` is in scope, thus attempt to resolve `y` as `A.y` is made and succeeds.
Finally, `lazy with (B)` has no identifiers to resolve.

--
Nov 23 2022