![]() ![]() ![]() Int.toShort() discard the highest bits of a number.Double.toInt() clamps a number into the Int range,.The suggested replacement is more explicit, because it has two conversion steps, Double->Int and Int->Short, and each of them is dealing with values out of the target type range in its own way: Forcing coders to write more verbose (and thus less readable) code that still suffers from exactly the same shortcomings is not, IMHO, an improvement. There is an valid argument that overflows should be detected when converting from one data type to another that cannot represent the full range of values in the original, but this would be better done as a run-time exception when the value is truly unrepresentable rather than as a blanket prohibition of the conversion. Again this is not unexpected because it is how the JVM specifies it, but it’s quite arbitrary and thus to me much less intuitive than a truncation. Here the result isn’t related in any way to the original value, it’s simply the maximum value of Int. Println("double = $double, double.toInt() = $") If the motivation expressed in the issue were to be taken to its logical conclusion, then Double#toInt() would also be deprecated since it will produce results even more counter-intuitive than Double#toShort(), e.g. ![]() Whether this is “counter-intuitive” or not is a matter of opinion (to me it’s perfectly logical) but it is certainly not unexpected (because that’s how the JVM specifies it.) I suspect the issue author does not work in realms where Byte and Short are used very much (byte and bit manipulation is an area where Kotlin is still somewhat deficient in general.) I don’t see how this deprecation helps - the suggested replacement: val double = 129.0 ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |