Object InheritanceDoes anyone know of any languages which support inheritance/polymorphism from a specific instance of a class at runtime? The idea is something analogous to cloning the object (shallow or deep) to capture internal state, and deriving from it at the same time. There are a few variations on this idea. The first is inheritance and replacement - inherit from an instance override what you need, and replace said instance. Another is an extension of the PrototypePattern where you create an instance but need to do something such as add new functionality, while maintaining polymorphism AND internal state.
A codebase can be reworked to solve these problems in most cases, but a language feature this has a certain interest. Anyone know of anything? --BrianMcCallister
All inheritance in SelfLanguage is based on delegation to prototypes (see PrototypeBasedProgramming).
Vaguely related:
JavaScript is also very interesting PrototypeBasedLanguage. And you can implement inheritance using proto copy of the super class. Here is an example
/**
* Vector class
*/
function Vector(){
//constructor
this.current = 0;
this.elements = new Array();
// member functions
this.add = function(element) { elements.add(element); ++current };
}
/**
*Stack class inherits vector
*/
function Stack() {
//proto typical copy of base
this.base = Vector;
this.base();
//member functions
this.push = function(element) { this.add(element); //call to super };
}
-SeshKumar
Ruby allows you to do that (and I think SmallTalk as well, if I remember correctly). Here's a Ruby code snippet from ProgrammingRuby. It's pretty self explanatory, the built-in String::to_s method is overridden for object a only.
a = "hello"
b = a.dup
def a.to_s
"The value is '#{self}'"
end
def a.twoTimes
self + self
end
a.to_s » "The value is 'hello'"
a.twoTimes » "hellohello"
b.to_s » "hello"
--AndrewQueisser
EditText of this page
(last edited May 31, 2004)
or FindPage with title or text search