4

Is it possible to write something like in JavaScript

if( result = foo(); result.status == true)
{
//Code goes here
}

Comments
  • 10
    Why don‘t write?
    if( foo().status ){ ... }
  • 2
    Depends on what you think by adding ';' in the condition (it's still a syntax error)
    But it's possible to check if you can successfully pass a function's result to a variable

    Ex:
    let stuff = null;
    if(stuff = some_function(some, params)) {
    // Code
    }

    I've seen this once but I would not recommend that
  • 1
    @kimb result have other key too.
    Let say message
    Then to access message, ain't I again need to do the foo().message ?
  • 1
    @nihalmurmu if you know that foo() returns true, but it also can return something else, you could do
    if(foo() && foo() === true){
    // Code
    }
  • 5
    if((result = foo()).status == true)
    Should do
    Result needs to be defined beforehand, though
  • 3
    result = foo();
    if (result.status === true) {
    // wow it works
    }
  • 0
    @Drillan767
    But usually Foo returns Bar, I see that everywhere all the time, also returns Baz, my bad..
  • 0
    Why the hell do you want to write that?
    Why dont you just write:
    result = foo();
    if (result && result.status) {
    //...
    }
    Or to be sure
    result = foo();
    if ('result' in result && result.status) {
    //...
    }
Add Comment