8 ms·
Here's why you might not want to have type conversion. I ran into this today: class String { int find(char needle, int startIdx); }; I ended up calling:
by prewett 11y ago
Here's why you might not want to have type conversion. I ran into this today:
class String {
int find(char needle, int startIdx);
};
I ended up calling:
int idx = find(startIdx, ';');
C++ happily converted my char to an int, and my int to a char, no warning or anything. Surprisingly, the code appeared to work for months, and then when I upgrade Xcode to the beta, it started failing. Weird.
I end up casting stuff when I do math, anyway, because I can never remember when 'int op float' and 'float op int' results in an int or a float, and every time I don't it ends up as a truncated int when I wanted a float in [0, 1]. So, a little more painful to have to cast stuff around, but at least you find out quickly about my find() bug and you don't end up with something like
if (secondsFromMidnightInFloat / 86400 > .5) {...}
always fails. I can't tell you how much time I've wasted tracking something like that down, despite my diligence in adding (float). That and that stupid "->" for pointers drive me nuts. Even more than header files.
- kbenson 11y agoFrom the discussion linked[1] in another reply at your level, I believe the proposed automatic type conversions are only for when there's no data loss, and there needs to be explicit conversions whenever loss of information is possible. That is, it's implicit for i32 -> u32, or i32 -> i64, but not for something like i32 -> i8 (such as the int to char conversion you mentioned), which would need to be explicitly converted. 1: https://internals.rust-lang.org/t/implicit-widening-polymorphic-indexing-and-similar-ideas/1141 https://internals.rust-lang.org/t/implicit-widening-polymorp...
- blub 11y agoThe first case will be detected by -Wconversion, however you have to enable it. For the second case, correct me if I'm wrong, but I think both float / int and int / float have a float result. In fact, pretty much all operations involving an int and a float will have a float result.