convex icon indicating copy to clipboard operation
convex copied to clipboard

Should `boolean`, `byte` and `char` coerce to double implicitly?

Open helins opened this issue 4 years ago • 5 comments

This is what Clojure does as well, but I am wondering it isn't best to always avoid double coercions.

helins avatar Sep 30 '21 20:09 helins

Do you mean implicitly?

I think implicit widening is OK. Implicit loss of precision should probably be a no-no.

mikera avatar Oct 01 '21 02:10 mikera

Sorry there, went horribly wrong with the original title! This is about double, such as (double \a), hence the double conversion Char -> Integer -> Double

helins avatar Oct 21 '21 20:10 helins

I think the answer might be no in general? Multiple sequential conversions don't seem like something we should try to do implicitly, it could be dangerous.

We could make a case for it working with Byte though, since this is really an integer value already. As per #89

mikera avatar Oct 22 '21 23:10 mikera

I would advocate for no as well to be inline with previous similar decisions.

helins avatar Nov 04 '21 18:11 helins

I think it depends whether we want to count Byte as a numeric type or not.

Generally you can implicitly up-cast to larger numerics, so (+ 1 2.0) would be expected to work (returning a Double which is the "largest" numeric type involved).

So if Byte is numeric, it should implicitly cast to Long and Double. In which case (+ 1.0 (byte 2)) should work, as well as some interesting things like (get [1 2 3 4] (first 0x0203))

If Byte is not numeric, then of course explicit casts would be required to do the above.

mikera avatar Nov 05 '21 04:11 mikera