3 ms·
> how does that "map" to "classic" inheritance, from Java or C++ I'd recommend doing it in two steps. First, you need to get from the Java/C++ perspective of
by chriswarbo 5y ago
> how does that "map" to "classic" inheritance, from Java or C++
I'd recommend doing it in two steps.
First, you need to get from the Java/C++ perspective of "classes are compile-time templates" to a perspective of "everything is an object". Python is a good example of the latter, as is Smalltalk (the latter is more 'extreme', but also less widely used):
$ python3
Python 3.8.9 (default, May 27 2021, 03:09:34)
[Clang 7.1.0 (tags/RELEASE_710/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... pass
...
>>> myA = A()
>>> myA
<__main__.A object at 0x10670c490>
>>> A
<class '__main__.A'>
>>> myA.__class__
<class '__main__.A'>
>>> class B(A):
... x = 123
...
>>> myA.__class__ = B
>>> myA.x
123
>>>
Once you're comfortable with that, you can take the second step: from a Python/Smalltalk perspective of "everything is an object; when a slot isn't found in the instance, we fall back to looking in its class", to reach the Javascript/Self perspective of "everything is an object; when a slot isn't found in an object, it can tell us to look in another object". That 'other object' is the prototype.
All we're doing in the second step is removing a distinction: Python/Smalltalk have a built-in notion of "class", where some objects are classes and others aren't. Javascript/Self just have objects: we can use any object as a prototype for any other.
- sesm 5y agoThere is a big difference between Java and C++ classes. In C++ classes are not present at runtime, but in Java they are present at runtime, are accessible via reflection API and can be loaded dynamically at runtime.