Issue #15592 has been updated by fxn (Xavier Noria).


Many people do not realize that in order to eager load a project tree you need to autoload. Talking about the general case.

When Zeitwerk eager loads, it issues require calls, but the autoloads are set orderly still because otherwise it would not work generally.

In a Rails application, for example, you may have something like


``` ruby
module CountriesHelper
  ALL = Contry.all
end
```

or explicit namespaces as in

```ruby
class Hotel < ActiveRecord::Base
  include Hotel::Pricing
end
```

In the case of the proposal here, you could have (schematic):

```ruby
module M
  autoload :X
  autoload :Y
end
```

and then

```ruby
module M
  module X
    include Y
  end
end
```

You need to resolve a top-level M::Y before its autoload has been set.

The key observation is that _any_ constant can be referenced at the top-level of execution.

I think this proposal won't work in the general case.

----------------------------------------
Feature #15592: mode that "autoload" behaves "require" immediately
https://bugs.ruby-lang.org/issues/15592#change-76736

* Author: akr (Akira Tanaka)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
How about a feature to switch "autoload" behavior to "require" immediately.

autoload is a feature for lazy loading.

matz dislikes autoload as [Feature #5653].
I heard that he dislikes class (and other) definitions at arbitrary timing.
I agree that eager loading is safer than lazy loading.

However, lazy loading realize shorter loading time and
it makes development cycle shorter.
It is more important for larger applications as Eregon said in
https://bugs.ruby-lang.org/issues/5653#note-39 .
It is especially important when library loading causes I/O (code generation from DB schema).

These two, safety of eager loading and easier development of lazy loading, conflicts.
But if we can distinguish production mode and development mode,
we can enjoy both benefits.

So, I propose a feature to select autoload behavior from two modes:
- autoload behaves as lazy loading as now in development mode
- autoload behaves as eager loading (immediately invokes "require") in production mode.

There are several idea to switch the mode:

- $AUTOLOAD_MODE = :eager or :lazy
- RubyVM.autoload_mode = :eager or :lazy
- ObjectSpace.autoload_mode = :eager or lazy

I'm not sure there is a good enough one in above list, though.



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>