QML Duck-typing Example

When one wants to achieve dynamic behavior in object-oriented languages like C++ or Java one approach is to leverage inheritance; e.g., one could define a base class or an interface that defines a method, say “foo()”, that must be implemented by derived classes. Classes derived from that base class or implementing that interface need to provide an implementation for that “foo()” method. So, by this mechanism the “foo()” method can be called on all objects (instances) of classes deriving/implementing the base class/interface in question. Thereby, the actual implementation can vary arbitrarily.

In QML there is no need for such an inheritance-based approach; QML supports what is also referred to as “duck-typing”. In a nutshell, duck-typing is a way to resolve and call methods independently of type information. The only important thing is that, when calling a method, an actual method implementation exists, else an error will occur.

The term “duck-typing” originates from a quote that is attributed to James Whitcomb Riley: “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”. In the context of a programming language it can be roughly interpreted as: if I see an object (no matter of what type) with the method I want to call, I call that method.

In order to demonstrate this in action I created a simple stand-alone example. You can get the source code of this example on github. Following is a screenshot of what it looks like.

What this example does is fairly simple: the buttons at the top allow to dynamically create objects of different types. By pressing the “Execute” button it is attempted to execute/call a method “myMethod()” on the resulting object.

The first types resemble a classic C++ class hierarchy with base class A and B and C being derived from A. In A “myMethod” is declared as virtual and that is overwritten by B and C respectively.

D is a stand-alone class that does not provide an implementation of “myMethod()”. This demonstrates that errors will occur when it is attempted to call a method on an object (a class instance) for which no corresponding method implementation exists.

UVW is another stand-alone class but this time it provides a “myMethod()” method. This demonstrates that duck-typing generally works. We do not rely on any type information, just the fact that a “myMethod()” implementation exists.

Similarly, XYZ is yet another stand-alone class with a “myMethod()” method. However, this time “myMethod()” also returns an integer value. The value depends on the internal state of a XZY object and is incremented at each call. This demonstrates that by leveraging duck-typing in QML methods may or may not return values.

Types QRS and MNO are similar to UVW and XYZ except that these are implemented in QML insteadd of C++/Qt. This demonstrates that the duck-typing capabilities are not limited to classes created in C++/Qt.

Not shown in this example is dynamic object creation in C++/Qt; all objects are dynamically created in QML.

I wrote this example as reply to a question by Marcin.

This entry was posted in Qt/QML, Snippets and tagged , . Bookmark the permalink.

2 Responses to QML Duck-typing Example

  1. Marcin says:

    Thanks very much!!

  2. Marcin says:

    The problem is that I use the variable in many pages, and if I use the variable there, I get the error “can’t find variable”. Where should I put the creation of object (on startup first), so that it can be accessible on all the pages? Which object should be its parent?
    Thanks in advance

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.