CoffeeScript in qml, proof of concept.
-
In case anyone's interested, I hacked together a fork of qtdeclarative (based on dev) that allows coffeescript to be used in QML:
https://github.com/corngood/qtdeclarative.git
I've just been using it in a small hobby project, so I'm not making any promises about how well it works.
You can make a standalone script file with a .coffee extension, and add:
@.pragma language "coffeescript"@
Then use:
@import "test.coffee" as Test@
In qml, for property assignments and signal handlers, instead of using a colon, you can put a ->
@
Loader {
source -> if someFlag then "a.qml" else "b.qml"
Keys.onPressed ->
if event.key is Qt.Key_F1
doSomething()
event.accepted = true
}
@Everything after the -> will be parsed as a block of coffeescript until it finds a line that is equally or less indented than the line on which -> occurred. That's kind of dirty, but since coffeescript cares about indentation it doesn't bother me too much. Standard qml syntax will still be interpreted as javascript, and it's probably best to use it for ids/literals.
Something to be aware of is that coffeescript will not let you assign a qml global (e.g. property on current object) with "property = value", and will instead declare a local 'var property'. To work around this you'll need to assign an id to the object and use "id.property = value".
Things I've been thinking about for future work:
- hooks for qml/js translation in qtdeclarative, and coffeescript support in a plugin
- writing of translated qml/js (e.g. for build time translation)
- a pure coffeescript dialect of qml, using coffeescript's object notation
- modified coffeescript compiler that understands qml globals
- sourcemap support for debugging
-
nice.
-
Hm, that's an interesting development! Not that it makes much difference to me personally, I would really like to see proper language, like Python, instead of JS :)