[Development] Setters: Clarifying the ownership

Jaroslaw Kobus Jaroslaw.Kobus at qt.io
Fri Jan 19 17:09:21 CET 2018


"give" may be confused with "get", which is usually an accessor. I may also think "Am I giving (to QCoreApplication)" or "The QCoreApplication is giving (me)". Maybe it is just a matter of the other verb? Absorb, hand over, hand on, suck in, swallow...


Jarek

________________________________
From: Development <development-bounces+jaroslaw.kobus=qt.io at qt-project.org> on behalf of Jesus Fernandez <jesus.fernandez at qt.io>
Sent: Friday, January 19, 2018 4:15:52 PM
To: development at qt-project.org
Subject: [Development] Setters: Clarifying the ownership

Hi all!

I always found something annoying in the Qt API. The problem comes with the setters of our properties. When I want to pass an object to a property I never know if I need to take care of the object or relay on parenting system to avoid memory leaks.
To know if the object is going to be reparented, I open the assistant and look for the setter to try to find the famous "takes ownership of" in the function description.

Mårten Nordheim and I were talking about possible solutions to this problem. Typical things came to the discussion:
- adding a macro like Q_TAKES_OWNERSHIP to the function that expands to nothing
- wrapping the parameters with a template class (gsl::owner<T>)
- ...

After some discussion he came with the idea of add a different "verb" to the setter, replace "set" with "give". So when we are giving the ownership of an object to instead of setSomething(&object); we will write giveSomething(&object); I really like this solution, it will improve a lot the readability of the client (and internal) code.

For example: QCoreApplication::setEventDispatcher will be QCoreApplication::giveEventDispatcher.

Of course at the beginning this will be a new function and the old set* functions will be kept, but marked as deprecated.


--
Best regards,
Jesús
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20180119/663b1c5a/attachment.html>


More information about the Development mailing list