[#71931] [Ruby trunk - Feature #11786] [Open] [PATCH] micro-optimize case dispatch even harder — normalperson@...

Issue #11786 has been reported by Eric Wong.

9 messages 2015/12/08

[ruby-core:72563] [Ruby trunk - Feature #11911] [Open] Immutable method definitions and/or static dispatch

From: matt.larraz@...
Date: 2015-12-28 18:17:51 UTC
List: ruby-core #72563
Issue #11911 has been reported by Matt Larraz.

----------------------------------------
Feature #11911: Immutable method definitions and/or static dispatch
https://bugs.ruby-lang.org/issues/11911

* Author: Matt Larraz
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
One of Ruby's biggest strengths is the ability for anyone, at any time, to redefine (almost) any behavior. But this is also one of its biggest weaknesses.
Ruby has a very liberal dynamic dispatch, so any method can be redefined anywhere in the code, meaning we can never have any guarantees about behavior.

Other languages with dynamic dispatch (like C++ or Java) also allow for static dispatch. In particular, Java has dynamic dispatch by default, with the `final` keyword marking a method as immutable.

In Ruby, this might look something like:

~~~ruby
def foo
 'foo'
end

final :foo

# Raises an exception
def foo
 'bar'
end
~~~

I see this as analogous to freezing a string.

Note that if somebody really needs to overwrite an immutable method, they can still do so, just in a more explicit way:

~~~ruby
undef_method :foo

# Works as expected
def foo
 'bar'
end
~~~
This eliminates some ambiguity.

I'm not sure how feasible this is (or whether this is the ideal syntax), but I'd like to hear what the community thinks of such a concept in general.



-- 
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>

In This Thread

Prev Next