> Thinking about it, this becomes something more like a "with" keyword

I was starting to think along these lines.

I do like the idea of being able to specify the context for a block -
though, as you say, this feature raises issues, and in e.g.
Javascript, these days, it is now strongly discouraged, and prevented
in strict mode.

I do think it would have some potentially pretty awesome uses though -
for example, in templates, assuming this statement would have a
non-block counterpart (like if and while have) you could "import" a
class with HTML helper methods in this way:

    <?php with ($html_helper): ?>
    <h1><?= escape($title) ?></h1>
    <?php endwith ?>

It also potentially lets you create highly convenient DSLs:

    with ($parser_helper) {
        $INT = seq(optional(char("-")), plus(digit()));
    }

This would have potentially really good IDE support and offline
inspections as well.

I guess the biggest issue is what happens when you wrap multiple scopes?

    with ($a) {
        with ($b) {
            foo();
        }
    }

What happens in this case? I don't think there's any good answer to
that question - if neither $b or $a implements foo(), the only thing
the compiler could do, is dispatch $b->__call("foo", []) ... so you'd
need some really complex logic and rules, and these constructs could
have some pretty unpredictable performance implications as well.

There's also the question of what happens in this case:

    with ($a) {
        bar();
    }

    function bar() {
        with ($b) {
            fud();
        }
    }

Does it work in the local scope only, or would it be able to resolve
as $a->fud() ? That's crazy, and it probably should work only in the
lexical scope of the current function, but anyways - I think this
feature raises a lot of ugly questions with no elegant answers...


On Tue, Jul 12, 2016 at 10:36 AM, Rowan Collins <rowan.coll...@gmail.com> wrote:
> On 12/07/2016 00:14, Jesse Schalken wrote:
>>
>> If so, some consideration should be made as to which syntax is preferred
>> to solve this problem generally for both setting properties and calling
>> methods.
>>
>> I prefer the "obj { .. }" syntax because it mirrors object literals in
>> JSON/JavaScript, object initializers in C# and C/C++ and Haskell record
>> syntax, is particularly concise when nested to create large trees of
>> objects, and avoids the noise of "->>" on each line.
>
>
> All the examples you've given for that syntax are for initialisation, and
> I'm not sure how readable it would be for the general "do several things in
> a row" case:
>
> $counter {
>    count(),
>    count(),
>    count(),
>    count()
> }
>
> Thinking about it, this becomes something more like a "with" keyword:
>
> with ( $counter ) {
>    count();
>    count();
>    count();
>    count();
> }
>
> If with(){} could be used as an expression, you'd get something remarkably
> close to your initialiser example:
>
> $this->setBlah(
>     with ( Blah::create(4) ) {
>             foo = $foo;
>             baz = with ( new Baz() ) {
>                 markFixed();
>                 label = "Hello";
>             };
>             setBot(9);
>     }
> );
>
> I'm not sure if this is a good thing or not - with() statements exist in a
> few languages, with varying degrees of acceptance. I don't remember seeing
> one used as an expression like that before, though.
>
> There's a definite awkwardness of what to put on the left side of property
> assignments too...
>
> with ( new Foo ) { var = 1 }
> with ( new Foo ) { $var = 1 }
> with ( new Foo ) { ->var = 1 }
>
>
> Overall, it's an interesting idea, but the details are fiddly, and the gains
> marginal, IMO.
>
>
> Regards,
> --
> Rowan Collins
> [IMSoP]
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to