Re: RE: RFC: expectations/assertions

From: Date: Wed, 05 Feb 2014 10:08:11 +0000
Subject: Re: RE: RFC: expectations/assertions
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Yasuo Ohgaki wrote (on 05/02/2014):
JavaScript implementations have different APIs. Node.js has closer API. [...] assert( function() {
     // Some check here
     return FALSE;
}, 'Error' ); I would use at least, since I don't want to assert() related code to use variable scope nor namespace.
Actually, if you think this through with a real example, this only makes sense because of JS's scoping rules, which are very different from PHP's. A JS closure inherits the whole parent scope by default, and adds private vars to it, so this would be possible: function foo(arg) {
    assert ( function() {
        var assert_private_var = some_function();
        return arg == assert_private_var;
    }, 'oops' );
} In PHP, there is no notion of inherited scope, so this would *not* work: function foo($arg) {
    assert ( function() {
        $assert_private_var = some_function();
        return $arg == $assert_private_var;
    }, 'oops' );
} Since an assertion that can't actually check any state is useless for anything other than assert(false, 'Should never happen'), you'd always have to have a use() importing things: function foo($arg) {
    assert ( function() use ($arg) {
        $assert_private_var = some_function();
        return $arg == $assert_private_var;
    }, 'oops' );
} That's a lot of boilerplate and room for mistakes, IMHO. Of course, if we could fix the parser to evaluate functions as soon as they're defined like in JS, you could make the whole thing into an expression which would work with the current (and proposed) assert: function foo($arg) {
    assert ( (function() use ($arg) {
        $assert_private_var = some_function();
        return $arg == $assert_private_var;
    })(), 'oops' );
} Not pretty, but it would give you your local scope. Regards, -- Rowan Collins [IMSoP]

Thread (44 messages)

« previous php.internals (#72256) next »