[Interest] Set manipulation in Qt 6

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Sun Jun 21 22:28:46 CEST 2020


Il 21/06/20 20:07, Elvis Stansvik ha scritto:
> I'm all for Qt offering faster way to do things. I expect that. But I
> also wonder: Why remove the slower alternative for those who value
> readability over optimality in the cases where the extra performance
> is not needed? In the docs, the slow alternative could have pointers
> to the fast alternative, and the user could make an informed choice.

Let me elaborate on what I commented on the bug report:

1) no API should lead to naturally writing bad code;

2) no API should encourage premature pessimization;

3) APIs should encourage the natural discovery of the best tool for a 
given job. If your job is to remove duplicates, the API should favour an 
algorithm for that, not the toSet().toList() combo, which is just an 
antipattern!

4) as developers of Qt: we must care about all of the above; and bad 
habits on user side shouldn't result in bad APIs, in the name of 
convenience or whatever;

5) ultimately, the most direct replacement of toContainer() is

> container | ranges::to<AnotherContainer> 

which isn't that much of an extra burden to learn / to type rather than 
toContainer(), and it's also obviously more general.

---

In other words: if you "don't care", you can just replace list.toSet() 
with list | ranges::to<QSet>. You can do it today, using just a C++14 
compiler (and you need a C++17 compiler anyhow if you're aiming for Qt 6).

Back on to OP's issue, with this code using deprecated APIs:

>     QSet<QString> missingKeys{customLines.keys().toSet().subtract(customLinesColor.keys().toSet())};

Then the "don't care" port can be done by something like:

> QSet<QString> missingKeys = customLines.keys() | ranges::to<QSet>;
> missingKeys.subtract(customLinesColor.keys() | ranges::to<QSet>);


If nobody ever told you about toSet(), and only told you about 
ranges::to<X>, would you be upset by toSet()'s deprecation?


My 2 c,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4329 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200621/a3f30b8b/attachment.bin>


More information about the Interest mailing list