From: "Dan0042 (Daniel DeLorme) via ruby-core" Date: 2023-10-18T17:45:07+00:00 Subject: [ruby-core:115093] [Ruby master Misc#19931] to_int is not for implicit conversion? Issue #19931 has been updated by Dan0042 (Daniel DeLorme). byroot (Jean Boussier) wrote in #note-6: > I have no idea how much code it would break though. It can't *break* code. It can only cause broken code (as in my example) to suddenly work. Eregon (Benoit Daloze) wrote in #note-7: > I think the coercion semantics are already quite complex, so we would need a convincing real-world use-case to make this more complicated. I think it would make things simpler. For `1 + obj` you'd only have to define #to_int instead of the much more complex #coerce. And it would smooth out the hard-to-explain discrepancy that to_str/to_ary/to_hash allow implicit conversion but somehow to_int doesn't. But that's only if the current behavior of to_int wasn't deliberate. And if it was I'd just like to know the rationale. ---------------------------------------- Misc #19931: to_int is not for implicit conversion? https://bugs.ruby-lang.org/issues/19931#change-104997 * Author: Dan0042 (Daniel DeLorme) * Status: Open * Priority: Normal ---------------------------------------- While reviewing some implicit vs explicit conversion concepts, I discovered that arithmetic operations do not perform the implicit conversion I expected from #to_int ```ruby o = Object.new def o.to_int; 1; end 1 + o #TypeError ``` I understand there's the whole #coerce thing for numbers, but I had expected #to_int to fit neatly into this and cause the object to be implicitly coerced to Integer. So basically I thought that #to_i was for explicit conversion and #to_int for implicit conversion; is that not the case? Most of the internet seems to think that (to_int : to_i) relationship is like (to_str : to_s). But I can't seems to find authoritative documentation on the topic. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/