This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Editorial: Phrase private methods as brands, not fields #52
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
littledan
force-pushed
the
private-brands
branch
from
December 11, 2018 17:50
9b87205
to
0aea53b
Compare
littledan
added a commit
to tc39/proposal-static-class-features
that referenced
this pull request
Dec 11, 2018
gsathya
reviewed
Dec 11, 2018
spec.html
Outdated
<emu-alg> | ||
1. If _O_.[[PrivateBrands]] contains an entry _e_ such that SameValue(_e_, _brand_) is *true*, | ||
1. Throw a *TypeError* exception. | ||
1. Append _P_ to _O_.[[PrivateBrands]]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is P?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that should say brand, thanks, will fix.
Many people attempting to implement private methods, both in transpilers and native JS engines, have run into similar sources of confusion about semantics and optimizability: - Many have the misconception that each instance of a class with private methods has its own copy of that method, as if it were written in a field initializer, when actually, the same function identity is intended. - Private methods are designed to have semantics that they can be implemented as a single check that the receiver "has" the private method, followed by a call of that method, without taking up memory space per-instance. However, many implementers are reaching for a direct implementation of them as non-writable private fields, which ends up taking a large amount of additional space. To clear things up, this patch rephrases private methods and accessors' semantics as being based on a brand check, followed by a function call. Each object has a list of "private brands" it supports, which are identified by Klass.prototype, for classes Klass which have private methods or accessors. Private names are considered to have both a reference to this brand as well as the actual method linked from them. My hope is that this will be a reasonable first-pass implementation strategy; there is a note about how to optimize the brand list further. With this change, the static class features needs some simple changes, and the decorators proposal needs some larger changes. See tc39/proposal-decorators#180 for details.
littledan
force-pushed
the
private-brands
branch
from
December 12, 2018 18:49
0aea53b
to
3e1b9e9
Compare
OK, I'm going to land this patch so it's more easy to read this version. If anyone has concerns with it, please say so here, and I can roll it back. |
littledan
added a commit
to tc39/proposal-static-class-features
that referenced
this pull request
Dec 12, 2018
47 tasks
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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 applied 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.
Many people attempting to implement private methods, both in
transpilers and native JS engines, have run into similar sources
of confusion about semantics and optimizability:
private methods has its own copy of that method, as if it were
written in a field initializer, when actually, the same function
identity is intended.
implemented as a single check that the receiver "has" the private
method, followed by a call of that method, without taking up
memory space per-instance. However, many implementers are reaching
for a direct implementation of them as non-writable private fields,
which ends up taking a large amount of additional space.
To clear things up, this patch rephrases private methods and accessors'
semantics as being based on a brand check, followed by a function call.
Each object has a list of "private brands" it supports, which are
identified by Klass.prototype, for classes Klass which have private
methods or accessors. Private names are considered to have both a
reference to this brand as well as the actual method linked from them.
My hope is that this will be a reasonable first-pass implementation
strategy; there is a note about how to optimize the brand list further.
With this change, the static class features needs some simple changes,
and the decorators proposal needs some larger changes. See
tc39/proposal-decorators#180 for details.