From: "fxn (Xavier Noria)" Date: 2022-03-16T09:58:47+00:00 Subject: [ruby-core:107920] [Ruby master Feature#15592] mode where "autoload" behaves like an immediate "require" Issue #15592 has been updated by fxn (Xavier Noria). I'd personally suggest closing this one. When you offer lazy/eager loading mechanisms, loading has to be _consistent_. You cannot have a code base that loads in lazy model, but errs on eager mode. ```ruby # m.rb module M autoload :X, 'm/x' autoload :Y, 'm/y' end ``` makes it hard. Because `m/x.rb` may be referencing `M::Y` at the top-level, and the autoload is not yet set in eager mode. And that is only one synthetic example, it can get more subtle, the issue may be 5 levels down, and may be introduced in a code base without you even noticing. The general problem is that in order to eager load without explicit `require`s, you need all the autoloads of a certain namespace in place. Accomplishing this is one key aspect of the implementation of Zeitwerk. ---------------------------------------- Feature #15592: mode where "autoload" behaves like an immediate "require" https://bugs.ruby-lang.org/issues/15592#change-96860 * Author: akr (Akira Tanaka) * Status: Open * Priority: Normal ---------------------------------------- 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: