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

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.



(4 comments) - (Post a new comment)


[info]dagbrown
2009-10-14 08:12 pm UTC (link)
First-method-definition-wins is counterintuitive behaviour for an interpreted language. A lot of things about Ruby become very easy to understand when you realize it's interpreted. Your proposed behaviour only makes sense if you think that Ruby is a compiled language--if you want to make that impression, you'll have to change a lot of other things as well. It wouldn't improve the language, in my opinion.

(Reply to this) (Thread)


[info]djberg96
2009-10-14 09:29 pm UTC (link)
You could still retain the current behavior (last method definition wins) and have the behavior I mentioned above. I don't think they're mutually exclusive.

(Reply to this) (Parent)(Thread)


[info]lesliev
2009-10-22 04:15 pm UTC (link)
I agree with last-definition-wins too, since then the final programmer gets the final say on what gets overridden. Kinda analogous to CSS.

Auto-namespacing sounds good to me, very convenient, but perhaps there's a nicer syntax? The module name on the end feels bad (and you changed the case - on purpose?)

(Reply to this) (Parent)(Thread)


[info]djberg96
2009-10-24 10:16 pm UTC (link)
If you can come up with a nicer syntax please let me know. I think some people prefer a blockier, more verbose syntax.

Yeah, I guess it should be t.hello:Alpha and t.hello:Beta.

(Reply to this) (Parent)


(4 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…