haXe Forum > [inline] What are "final returns"?

  • Hello, guys!

    I have read an article Inline and have been confused by "final returns" term. What does it means? And why next code is refused?

    inline function baz(flag)
    {
        if( flag )
            return 0;
        return 1;
    } // refused

    What is significant difference with next accepted code?
    inline function bar(flag)
    {
        if( flag )
            return 0;
        else
        return 1;
    } // accepted

    regards,
    Alex

  • First code is refused because you must define return value at one place, for example this will not work:

    public static inline function bar(flag1, flag2)
    {
    if (flag1) return 1;

    if (flag2) return 2;
    else return 0;
    }

    but this will

    public static inline function bar(flag1, flag2)
    {
    if (flag1) return 1;
    else if (flag2) return 2;
    else return 0;
    }

    so "final returns" are returns which must be final.

  • If you are in doubt as to if a return is final or not, just think in these terms:

    "Is there anything happening after this return?"

    If yes, it's not a final return, if no, it's is a final return.

    In your first example it's not a final return because there is another return futher down.

    In your second example it's a final return because after the whole "if" is done, there is nothing else that is happening. (if/else if/else counts as one big thing)

< Prev | Page 1 / 1 | Next >