Implementation of a dynamic prototype-based programming in Red #3064
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be ap
2CAA
plied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Red implement a prototype-based programming (value-share), i.e., the clones will be created from a prototype, but after creation they stand on their own feet (creation-time sharing).
See the paper of this link.
This addon of Red tries to implement a dynamic prototype-based programming (property-share), transparent to users and usual rules of Red.
This implementation following the concepts of the Self implementation of the object oriented programming language. Then, it uses objects and slots concept, i.e., it does not distinct variables from methods to be more flexible. Thus it also follow the philosophy of Red.
However, to explain the implementation, the difference between variables (properties) and methods (functions) is necessary.
Properties (variables)
Delegation (1): to share the value of a property
The delegation is clean and follow the rules of Red. The only difference is the slot
_proto
(or any other name decided by the main contributors of Red as_is
) to clone an object when we declare as prototype-based object from any otherobject!
(with or whithout prototype).It is not necessary the slot
_proto
to clone a siblind prototype following the rules of Red, but copy all other variables of the siblind object, if this conduct is not desirable then the user can use the usual creation of a prototype-based object.CPU cost
The cpu cost of this implementation to other users of
object!
is zero. However, a bad programming conduct could lead to crash the systemTo avoid it, is necessary check
_proto
as a validobject!
. There are two possibilities:Zero cost for object users and high cost for prototype users.
It is possible checking the validity of
_proto
in the delegation process, but the pay for the prototype users is in all delegations (variable access) of a prototype-based object.Cost sharing for all users.
The checking of validity of
_proto
is placed in the creation ofobject!
.I implement this option because the cost for all users is low only when
make
the object. After that, no aditional cost for any users at this moment.Delegation (2): new value of a property
Logically, any object can change the values of their properties but the question is if an object can modify the value of a prototype property or if can add new properties. This feature could be resolve with three mechanisms, from low to high cpu cost:
Objects can modify the slot values of the prototypes but no add new properties.
It is the more easy to implement without any aditional cost to the users. However, I think, this is a very risky option.
Objects can not add or modify properties of theirs prototypes.
This could be the default mechanism when the objects are static after creation. An object only can modify their properties. This idea is nowdays in the background of Red because a
extend
function is not posible to add properties to the objects.Objects can override properties of the prototypes, creating property if it is necessary.
An object can add properties presents in their prototypes but can no add new properties (when
extend
will be implemented in Red, they will can althought the implementation is easy!).I implement this last option because is safe, flexible and the cpu cost is the same as the previous option.
Remove properties
In this moment Red does not implement a function to remove slots of an object. Then, it is not implemented but when it will be implemented to
object!
, it could be used in prototype-based programming with any change.Methods (functions)
To be implemented