slint icon indicating copy to clipboard operation
slint copied to clipboard

Document when implicit type conversions happen

Open abwitek opened this issue 1 year ago • 3 comments

Hello devs,

I believe this is a feature request, although I may have overlooked something.

From what I understand reading the typing documentation of the Slint Language, a string is not convertible to int.

The only way I see to do it natively, correct me if I'm wrong, is to convert string to float (using to-float) and implicitly convert that to int.

I tend to mistrust float-to-int conversions. (Moreover, but that's a minor point, the doc as-is doesn't explicit what kind of rounding or truncating is used, which leave the option of tracking into the code base where and how that conversion is actually done, which is frankly far above my spoiled-and-clueless-end-user pay-grade.)

The solution I used was to implement the conversion in the my application's language and expose that part through a callback to the UI. This is much effort for a task that could be done more simply by the UI, for a feature that seems pretty basic and useful.

Are there a to-int and is-int functions planned?

If not, are there other ways to do what I need?

FYI, my actual use case is a LineEdit accepting only integral numbers whose value is used to fill up an int property elsewhere. I can restrict input by binding edited( string ) and then convert the text value with a callback bound on accepted( string ).

abwitek avatar May 04 '23 11:05 abwitek

For now you can do round(to-float())

I'm not sure it make sense to have explicit conversion. But we should indeed document what happens when conversion happens. I believe it truncate IIRC

ogoffart avatar May 04 '23 13:05 ogoffart

It looks like this is covered by https://slint.dev/releases/1.3.2/docs/slint/src/language/syntax/types#conversions . @ogoffart is that what you had in mind when you labelled this?

tronical avatar Jan 23 '24 13:01 tronical

I think we don't document what happens when converting from float to int. Is it rounded or not?

Also imho the to-float and co. are hard to find. Perhaps it should be in the string section.

ogoffart avatar Jan 23 '24 13:01 ogoffart