ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "joel@drapper.me (Joel Drapper)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109887] [Ruby master Feature#18798] `UnboundMethod#==` with inherited classes
Date: Sun, 11 Sep 2022 10:45:59 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-99128.20220911104559.17@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18798.20220524015549.17@ruby-lang.org

Issue #18798 has been updated by joel@drapper.me (Joel Drapper).


This would be really helpful for checking if a class has redefined a method inherited form a superclass.

As an example, I’m working on a compiler that replaces certain method calls with their inlined expected behaviour from an abstract superclass. Because it's possible for the subclass to override these abstract methods, the compiler should check if `instance_method(name) == AbstractClass.instance_method(name)` before folding it. While this wouldn't cover the method being overridden in the singleton class, that's a reasonable concession for my specific use case.

----------------------------------------
Feature #18798: `UnboundMethod#==` with inherited classes
https://bugs.ruby-lang.org/issues/18798#change-99128

* Author: ko1 (Koichi Sasada)
* Status: Open
* Priority: Normal
----------------------------------------
Now `UnboundMethod` for a same method from a superclass and an inherited class are not `==`.

```ruby
class C
  def foo = :C
  $mc = instance_method(:foo)
end

class D < C
  $md = instance_method(:foo)
end

p $mc == $md #=> false
p $mc.owner #=> C
p $mc.owner == $md.owner #=> true
p $mc.source_location == $md.source_location #=> true
p $mc.inspect #=> "#<UnboundMethod: C#foo() t.rb:3>"
p $md.inspect #=> "#<UnboundMethod: D(C)#foo() t.rb:3>"
```

How about to make it `UnboundMethod#==` return true for this case?
Rule: "return true if the UnboundMethod objects point to a same method definition" seems simple.

FYI: On aliased unbound methods point to a same method are `==`.


```ruby
class C
  def foo = :C
  alias bar foo
  $mfoo = instance_method(:foo)
  $mbar = instance_method(:bar)
end

p $mfoo, $mbar
#=> #<UnboundMethod: C#foo() t.rb:2>
#=> #<UnboundMethod: C#bar(foo)() t.rb:2>

p $mfoo == $mbar #=> true
```




-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2022-09-11 10:46 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-24  1:55 [ruby-core:108659] [Ruby master Bug#18798] `UnboundMethod#==` with inherited classes ko1 (Koichi Sasada)
2022-05-24  6:40 ` [ruby-core:108662] " sawa (Tsuyoshi Sawada)
2022-05-24 19:18 ` [ruby-core:108674] " jeremyevans0 (Jeremy Evans)
2022-05-24 19:49 ` [ruby-core:108676] " Eregon (Benoit Daloze)
2022-05-24 19:50 ` [ruby-core:108677] " Eregon (Benoit Daloze)
2022-05-25  0:28 ` [ruby-core:108688] [Ruby master Feature#18798] " ko1 (Koichi Sasada)
2022-05-25  0:32 ` [ruby-core:108689] " ko1 (Koichi Sasada)
2022-08-20 19:48 ` [ruby-core:109602] " Eregon (Benoit Daloze)
2022-09-11 10:45 ` joel@drapper.me (Joel Drapper) [this message]
2022-10-06  6:07 ` [ruby-core:110197] " matz (Yukihiro Matsumoto)
2022-10-06 12:00 ` [ruby-core:110207] " Eregon (Benoit Daloze)
2022-10-06 12:07 ` [ruby-core:110208] " Eregon (Benoit Daloze)
2022-10-06 14:29 ` [ruby-core:110209] " mame (Yusuke Endoh)
2022-10-06 16:20 ` [ruby-core:110215] " Eregon (Benoit Daloze)
2022-12-01  5:48 ` [ruby-core:111106] " matz (Yukihiro Matsumoto)
2022-12-06  9:13 ` [ruby-core:111219] " ko1 (Koichi Sasada)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-99128.20220911104559.17@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).