[Kde-pim] foreach and akonadi

Volker Krause vkrause at kde.org
Mon Apr 28 18:21:47 BST 2008


On Sunday 27 April 2008 19:32:17 Tom Albers wrote:
> Hi There,
>
> Albert is verifying the usage of foreach in kde's repository and send me
> the following interesting mail and ask me to forward it to the list.
>
> Toma
>
> Op zaterdag 26 april 2008 01:00 schreef Albert Astals Cid <aacid at kde.org>:
> > Doing my routine foreach cleanup i discovered a few places on akonadi
> > where foreach is used to modify the contents of a list
> >
> > Example:
> >     foreach ( Part part, parts ) {
> >       part.setData( QByteArray() );
> >       if ( !part.update() )
> >         qDebug() << "failed to update item part" << part.id();
> >     }
> >
> > The problem here is that you are calling a "modifier" on "part" variable,
> > that "part" is a copy of the element pressent on "parts", so it is really
> > not modifying anything once you go out of the foreach (see my blog [1])
> >
> > But NO! Will you say, it is working!

Right :) It is working because the intent here is not to modify the elements 
in the list (which are references to records in the database), but to work 
with them. The code might look different though ;-)

> > Yeah, i know it is working, it works "by luck" because on the Part copy
> > constructor you share the private pointer with the original element so in
> > the end, calling setData modifies the private pointer of the original
> > parts element, so it works.

With the usual implicitly shared semantics as known from Qt this wouldn't work 
either since calling the (non-const) setData() method would create a real 
copy of the shared private data (copy on write) which would be discarded when 
leaving the loop.

> > But in my opinion you should try to avoid these kind of foreach
> > constructions as foreach 99.99% does not work for modyfiyng lists and it
> > makes newbies to akonady code like me that look at it, think that it is
> > wrongly written.

I fully agree with that.

regards
Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-pim/attachments/20080428/f3a26e7a/attachment.sig>
-------------- next part --------------
_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/


More information about the kde-pim mailing list