enable Float to percentage conversions#10980
enable Float to percentage conversions#10980Murmele wants to merge 3 commits intoslint-ui:masterfrom
Conversation
Description: This allows to do calculations on relative values by using Floats and converting then to percentage by *1% or by dividing by 100. Example: ```slint private property <float> f: 50; private property <percent> p: (f / 2) * 1%; ``` Fixes: slint-ui#3785
|
We need to discuss this when I'm back from vacation. I'm a bit worried that the implicit conversion may cause problem for cases where the behaviour is different depending on Whether the type is a % or not (eg, colour functions or width/height.) |
Because of that I assigned to you. It is no rush to merge it :) |
|
Another reason i'm not in favor of this is that code like that: private property <percent> p: 50;It looks like it is 50%, but it is instead 5000% This means that forgetting an unit no longer result in a compilation error but now gives the wrong result, which is against the principle of type safety. Ideally we should have the following to work: private property <float> f: 50;
private property <percent> p: (f / 2) * 1%;But it doesn't, because the multiplication by 1% does't change the type and maybe it should. In other word, we should instead change the type of an expression that multiplies by a percentage, rather than having an automatic conversion. |
This is a good point, I will create later some tests regarding those cases so we don't forget them. Do you think it would make sense to create an explicit function for it? Like float_to_percent() instead of multiplying by *1% |
|
I think |
Fine for me. I will check it out next days |
Description: This allows to do calculations on relative values by using Floats and converting then to percentage by *1% or by dividing by 100. Example:
Fixes: #3785