ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: matz@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:83019] [Ruby trunk Feature#12533] Refinements: allow modules inclusion, in which the module can call internal methods which it defines.
Date: Tue, 26 Sep 2017 08:34:04 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-66931.20170926083403.42e9ff3f2e1d7c61@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-12533.20160629121143@ruby-lang.org

Issue #12533 has been updated by matz (Yukihiro Matsumoto).


As you may know, `include` inserts the module in the inheritance hierarchy. In this case, `module Extensions` is inserted above `String` in the `using Refinary` scope. That means the lexical scope of `vegetables` and `potatoes` are different from the refined scope so that `potatoes` cannot be called from `vegetables` because the scope is not refined. The situation is a bit complex. Do you follow me?

In some other class extension proposals found in other languages (for example, ClassBox in Java), scopes of methods called from within ClassBox are also modified. This is called local rebinding. But we don't choose that because it has bigger side effects. We chose the safer side.

The issue is by `include` we might expect the features from the included module are available but in fact, they aren't due to the mechanism of `include` and refinements.

So I counter-propose a new feature, `Module#inject`. Unlike `include`, `inject` does not modify inheritance hierarchy. Instead, it copies attributes (constants, modules, and refinements) into the target class/module.


shevegen,
We have no concrete plan to refine the refinement. We are vaguely thinking about combining `require` and `using` in some way. Just idea.

Matz.


----------------------------------------
Feature #12533: Refinements: allow modules inclusion, in which the module can call internal methods which it defines. 
https://bugs.ruby-lang.org/issues/12533#change-66931

* Author: chucke (Tiago Cardoso)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Right now this isn't possible:

~~~ruby
module Extensions
  def vegetables ; potatoe ; end
  def potatoe ; "potatoe" ; end
end

module Refinary
  refine String do
  # this doesn't work
  include Extensions
  # this would work...
  # def vegetables ; potatoe ; end
  # def potatoe ; "potatoe" ; end
  end
end

using Refinary

puts "tomatoe".vegetables

#=> in <main>': undefined method 'vegetables' for "tomatoe":String
~~~

Wrongly reported as a bug [here](https://bugs.ruby-lang.org/issues/12514). 

According to Shugo Maeda, this was expected behaviour. I argued that this is the way most monkey-patches work, and if Refinements can't cover the use case of inserting a custom DSL which references itself in the classes it refines, it can't fully replace monkey-patches, which I read was the main reason Refinements have been added to the language. 



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

  parent reply	other threads:[~2017-09-26  8:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-12533.20160629121143@ruby-lang.org>
2016-06-29 12:11 ` [ruby-core:76197] [Ruby trunk Feature#12533] Refinements: allow modules inclusion, in which the module can call internal methods which it defines cardoso_tiago
2016-06-29 12:12 ` [ruby-core:76198] " cardoso_tiago
2016-06-29 13:32 ` [ruby-core:76205] " futu.fata
2016-06-29 13:34 ` [ruby-core:76206] " futu.fata
2016-07-08  7:22 ` [ruby-core:76315] [Ruby trunk Feature#12533][Assigned] " shugo
2017-06-13  0:27 ` [ruby-core:81655] [Ruby trunk Feature#12533] " shevegen
2017-09-25  7:05 ` [ruby-core:82968] " duerst
2017-09-25 12:28 ` [ruby-core:82991] " shyouhei
2017-09-26  8:34 ` matz [this message]
2017-10-06 11:59 ` [ruby-core:83159] " cardoso_tiago

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-66931.20170926083403.42e9ff3f2e1d7c61@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).