开发者

function in expression, result into temporary variable only accessible if the expression is true

开发者 https://www.devze.com 2023-04-06 07:13 出处:网络
When an expression is a function that returns some kind of value, is there a way to get it\'s processed value in a temporary variable, that can be accessed only within ma开发者_如何学Pythontching expr

When an expression is a function that returns some kind of value, is there a way to get it's processed value in a temporary variable, that can be accessed only within ma开发者_如何学Pythontching expression?

Let's make a random example.

function used in expressions:

function crazy($input){
    return $input * 5 - mt_rand(0, 5);
}

And how I see this should work:

if(crazy(2) > 5){
    $result = $crazyTemporaryVariable . ' crazies'; 
    // because calling crazy(2) here again will:
    // 1) process it again, 
    // 2) because of the `mt_rand();` return different value.
}else{
    $result = 'nothing can happen here, because $crazyTemporaryVariable is not set';
}

In other words, it should create a temporary variable if the expression is true, else, "nothing" is left behind.

I'm aware that this can be done with the function call before the expression and assigning it's returned value to a previously set variable. But, you have to create a new variable- I see a small, but still a performance drop, because if the expression doesn't meet the requirements- the variable is useless, but stays defined.

// we have our crazy function above, a new expression

$crazy = crazy(2);
if($crazy > 5){
    $result = $crazy . ' crazies';
}else{
    $result = 'anything...';
}

// or...

$result = $crazy > 5 ? $crazy . ' crazies' : 'anything';

A note, this is PHP environment where I haven't seen this behavior/possibility nor in any other language which I've come in contact with (there are not much tho').

One more thing, it nicely applies to loops too:

while(crazy(2) <= 5){
    $result = $crazyTemporaryVariable . ' crazies';
}

// where normally you'd have to change the variable value;
$crazy = crazy(2);
while($crazy <= 5){
    $result = $crazy . ' crazies';
    $crazy = crazy(2);
}

Note: $crazyTemporaryVariable is just an imaginary variable. And if I've overlooked it, I doubt that it's a variable, think it could be a processing time constant EXPRESSION_RESULT, or something along those lines. But because of my English limitations, I have no idea what to look for to find it.

So yes, the key here is- defined, therefore accessible, only if expression is true.

So the question is, are there languages that support this and if I've overlooked it in PHP how can I access it then + reference, please?

After writing all what's above, I rememebered about this behavior:

if(($crazy = crazy(2)) > 5){
    $result = $crazy . ' crazies';
}else{
    $result = $crazy . ' crazies, but... not crazy enough.';
}

This is almost what I'm looking for, but it's nearly the same as defining our variable before, and you can access the result of the function if the expression matches, therefore, it won't cut this time.

Thanks in advance!

imaginary example

Ternary and javascript..

// could be: (keep in mind, this would not work with our _crazy_ function, because of random number.)
var result = string && string.search(/\s/) ? string.substr(0, __tempvar__) : string; // not available
// ...but instead is
var result = string.search(/\s/g) > 0 ? string.substring(0, string.search(/\s/g)) : string; // +1 repeated function call that affects performance.


I see a small, but still a performance drop, because if the expression doesn't meet the requirements- the variable is useless, but stays defined.

Whether declaring a variable makes any performance difference at all depends on the language and its implementation. Whether the performance difference matters depends on the application; probably only if the variable refers to a very large object that is not destroyed/GC'd because it still has a reference. Variables themselves should be practically free in any half-decent language implementation.

In other words, it should create a temporary variable if the expression is true, else, "nothing" is left behind.

This makes your language's scoping rules very complicated. What you might want to do in a language with C-like scoping rules is:

# declare $result
{
    $crazy = crazy(2);
    if ($crazy > 5){
        $result = $crazy . ' crazies';
    } else {
        $result = 'anything...';
    }
    # end of scope for $crazy, so it disappears
    # and the object it refers to takes up no more space
}

So the question is, are there languages that support this

IIRC, Perl stores the value of the last expression (or something like that) in $_. I personally find that hideous. In short, you really shouldn't want this; there are simpler, more principled ways of dealing with this problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号