Issue #11911 has been updated by Martin Drst.

Status changed from Open to Feedback

Ruby's ability to change any method anytime, and C++/Java's ability to overwrite some methods in subclasses, are conceptionally quite different. Which one are you interested in, and why? What is your use case?

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

* Author: Matt Larraz
* Status: Feedback
* 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>