djberg96 ([info]djberg96) wrote,
@ 2009-10-14 13:05:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Current mood: contemplative

More random thoughts on mixins, selector namespaces
Ovid's journal got me thinking about the multi-mixin problem again. That got me thinking about selector namespaces again, too.

Consider the current mixin behavior:

module Alpha
  def hello
    puts "Alpha"
  end
end

module Beta
  def hello
    puts "Beta"
  end
end

class Tango
  include Alpha
  include Beta
end

Tango.new.hello # => "Beta"

Possible solutions:

* First method definition wins
* Last method definition wins, raises a warning
* No one wins, it's an error

And my new, crazy idea:

* First method definition wins, later methods are auto-namespaced

# Let's assume this syntax actually works
t = Tango.new
t.hello       # => "Alpha" (wins, first definition)
t.hello:alpha # => "Alpha" (same, but explicit)
t.hello:beta  # => "Beta"  (calls Beta module's method)

Thoughts?

Update: As per the comments below, you could still have "last definition wins" behavior with the above syntax. Also, "alpha" and "beta" should probably be capitalized in the last example above. I dunno, though. I kinda like the lowercase notation in that context.


Advertisement


(Read 4 comments)

Post a comment in response:

From:
(will be screened)
Help(will be screened)
Identity URL: 
Username:
Password:
Don't have an account? Create one now.
Subject:
No HTML allowed in subject
   Help
Message:

 
Notice! This user has turned on the option that logs your IP address when posting. Help
Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…