[Qt-qml] Semantics of 'return' in a JavaScript block in QML

Eduardo Fleury eduardo.fleury at openbossa.org
Wed Sep 15 15:15:31 CEST 2010


Hi Pertti.


2010/9/15 Pertti Kellomäki <pertti.kellomaki at nokia.com>

> Text {
>    text: { if (condition) {
>                  return "A";
>               }
>               return "B";
>             }
> }
>
> (...) in JavaScript, a return statement can only be used
> inside functions. Is the above syntax an extension of JavaScript, and if
> so, is it documented somewhere?
>

My understanding is that in QML script blocks are implicit functions, and
the above should be simply a more concise version of:

Text {
    function foobar() {
        if (condition)
            return "A";
        return "B";
    }

    text: foobar()
}

Note that there's also a third valid version:

Text {
    text: {
        if (condition)
             "A"
        else
             "B"
     }
}

where the last evaluated statement is used as return value. This is
specially useful for one-liners like the one below.

Text {
      text: condition ? "A" : "B"
}

Cheers,
Eduardo

-- 
Eduardo M. Fleury
OpenBossa - INdT
http://eduardofleury.com/
http://www.openbossa.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100915/c6a6ea93/attachment.html 


More information about the Qt-qml mailing list