From: Matthew Kerwin Date: 2012-04-21T12:15:50+09:00 Subject: Re: Why must I know whether I extend a class or a module? On 21/04/2012, Intransition wrote: > > > On Friday, April 20, 2012 8:12:37 PM UTC-4, Matthew Kerwin wrote: >> >> On 20 April 2012 13:53, Intransition wrote: >> > The forced distinction only serves to take that flexibility away from >> > the >> > programmer. >> >> Is there not overhead associated with that distinction? I haven't >> actually looked into it, but I'd assume that Modules are a lot more >> light-weight than Classes, because of the things that can't be done >> with them. >> > The Class class is subclass of Module and is almost devoid of it's own > methods. Fair enough. Must be a pragmatism issue then. Unless the cost of discovering and defining constructors and initializers is somehow more than we'd expect. What about multiple inheritance? Is/should it be valid to mix in a class instead of a module? Actually, what happens right now if you define a class with no initializer, and mix in a module that defines __initialize ? >> > There are other benefits too in that removing the distinction would >> > allow >> > auto instantiation, e.g. >> > >> > class Foo::Bar >> > end >> > >> > Would work just fine, even though no `Foo` has been defined separately. >> This >> > can be especially nice when requiring sub-files. >> >> I can see that this would be convenient, but it also makes me cringe. >> You're defining a namespace Foo without ever explicitly defining it. >> Why? What's its purpose? How and where is it documented? I can't >> make my gut instinct accept this as anything but a bad idea. > > Typically that would be the case -- the encasing module/class would be > defined *ultimately*. This way it just doesn't have to be done prior to > classes/module internal to it. > > For example in project I often need adapters. > > class MyApp::Adapters::Foo > class MyApp::Adapters::Bar > ... > > In the myapp/adapters.rb I've would like to write: > > require 'myapp/adapters/foo' > require 'myapp/adapters/bar' > > module MyApp::Adapters > ... > end > > But instead I have to do: > > module MyApp::Adapters > ... > end > > require 'myapp/adapters/foo' > require 'myapp/adapters/bar' > > I don't like requires at the bottom. Or the alternative: require 'myapp/adapters/core' require 'myapp/adapters/foo' require 'myapp/adapters/bar' module MyApp::Adapters # stuff that uses foo and bar end > Even so, there are times when it doesn't really matter. If for instance my > MyApp::Adapters module had no contents other then the adapter classes, then > it really makes no difference. I guess I have a hole in my instinctive response for the right approach to 'modules as a namespace', since I usually use them as interfaces/multiple-inheritance/mixins. -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/ "You'll never find a programming language that frees you from the burden of clarifying your ideas." - xkcd