Different access modifier for getter and setter
Could it be possible to implement different access modifier for getter and setter? This way the setter could be for example private/protected and the getter public. In some cases this in really useful when the value shall be read-only.
Example:
class MyClass {
private _myProp: any;
private set myProp(value: any) {
this._myProp = value;
}
public get myProp(): any {
return this._myProp;
}
}
#12
As #12 is closed with #6532 now it seems that this issue is out of scope.
Do you now have any plans on implementing different access modifiers?
Because readonly is not enough to solve what's described here as a property might be actually writable inside a class.
I believe I should also move my example here from related issue:
declare abstract class Emitter {
new (): Emitter;
on: (name:string, handler: (arg:any) => void) => void;
off: (name:string, handler: (arg:any) => void) => void;
protected emit: (name:string, arg:any) => void;
}
class Person extends Emitter {
constructor(name:string) {
super();
this.name = name;
}
private _name:string;
get name() {
return this._name;
}
set name(value) {
if (this._name !== value) {
this._name = value;
this.emit('change:name', value);
//this way is better
this.updatedAt = new Date();
//than this way
this.setUpdatedAt(new Date());
}
}
////
private _updatedAt:Date;
get updatedAt() {
return this._updatedAt;
}
private set updatedAt(value) { //Getter and setter do not agree in visibility
//some logic and a simple readonly (our absence of a setter) is not enough
if (this._updatedAt !== value) {
this._updatedAt = value;
this.emit('change:updatedAt', value);
}
}
//// method implementation - but what's the point in it?
private setUpdatedAt(value) {
if (this._updatedAt !== value) {
this._updatedAt = value;
this.emit('change:updatedAt', value);
}
}
}
const entity = new Person('Mike');
entity.on('change:updatedAt', console.log.bind(console));
entity.name = 'Thomas';
//but manually setting updatedAt should be forbidden
entity.updatedAt = new Date(); //restricted
Here, property updatedAt actually can have a setter but it should not be accessable outside of Person. Moreover this setter contains complex logic and a simple readonly or an absence of a setter is not enough.
I agree that private setter is just a syntax sugar for private methods with additional logic (like emitting) but I think it is inconsistent when part of logic related to a field is in a property (getter) and another in a method (private setter).
This way getters/setters are implemented in C#, I think it would be usefull to allow different visibility at compile time, though this contracts will be lost in runtime.
The recommendation here is to use a public readonly getter and a private/protected backing field.
Accessors are symetrical with properties in the type system. anything we do will need to be manifested in the type and expressible on properties. Adding new access modifiers to enable private_set/public_get would increase the complexity of the language and the learning curve, and the value gained from this would not match the added complexity.
Anyway adding my +1 for this, in case it is considered again in future...
I'd still like to see this. C# has it, and it's incredibly useful in many circumstances, especially when you have flags that want to be read externally to the class, but only set internally with private/protected.
I think the suggestion that it isn't used all that often is a farce in that the pattern isn't used because it's not available to use.
This is a good addition to the language: The amount of complexity that this would add (from the perspective of a typescript programmer) is small. The concept is easy to understand and the compiler should be able to provide good error messages hinting of why you cannot access getters or setters due to scoping.
I agree with the above users, this is standard for most other languages. The argument against it does not hold weight and the implementation should be reconsidered.
I actually was surprised you can not do this in TS... +1 to the issue. There shouldn't bee any increased learning curve with this
private get x() { ... }
public set x(value) { ... }
Imo if you can read english it is self-explanatory what private(protected)/public mean here. Also if you are defining the accessors in a first place - you probably already know what they are for.
P.s. About the error: "Getter and setter accessors do not agree in visibility" - well: that's exactly what I want them to do
Here are two use cases where this would be handy:
Backbone.js, to avoid ugly .get() and .set() calls:
class Whatever {
public get rotation(): number {
return this.get('rotation');
}
private set rotation(rotation: number) {
this.set('rotation', rotation);
}
}
Properties that subclasses can modify:
class ExtendMe {
public get someProperty(): string {
// some stuff
}
protected set someProperty(prop: string) {
// some stuff
}
}
I would definitely like to see this, as it has been bugging me since I started using TypeScript.
I agree with the people requesting this feature. I just tried having a public get() method and was surprised to see that my set and get must agree on visibility.
I've been reading you guys claiming it is too complex. Remembering the "C++ way" why it is different from:
private _myAttribute: string;
get myAttribute(): string {...}
setMyAttribute(value: string) {...}
I can see a lot of use cases for this, that's why I'm here right now. What if I want myAttribute to be publicly accessible but allow only to be modified inside its class? What if I want to add some custom logic each time the attribute is modified? It can be a business rule, it can be just a logging to understand why it has been assigned a specific value, together with a breakpoint condition for debugging purposes, etc...
Basically, we want a method to be used each time we modify the attribute together with the syntactic sugar that makes it seems we're just assigning a value, instead than calling a method. C# has the concept of properties for this and I thought TS inherited that concept, but not allowing different accessibilities is a big limitations for people that think like me and make us fall back to the "C++ style".
"It's too hard" should never be a reason to not implement an incredibly useful feature.
Adding my +1. It seems very odd to me that in a language that is so completely in so many regards you can't do this.
Surprised to find that this issue is marked as closed. This feature is DESIRED by the community. Please reopen. +1
#12 doesn't address this issue, so this issue should not be closed with a comment that refers to that issue.
IMHO this is a nice feature but as fair as I understand it's just the situation "well I do NOT wan't to write everytime _property instead I want to use private set property"
If there is choice either adding huge complexity to the TypeScript core code itself (remember, more unnecessary complexity - less speed of developing things, more time to debug, write tests, in the end - less frequent releases) but reducing writing 1 symbol OR writing that 1 symbol and deal with it well... I would rather write 1 symbol one more time every time. In the end, I'm no contributor to TS core and I definitely don't want to deal myself with possible complexity of this sugar.
It's not a dealbreaker guys. Also it takes courage and wisdom to spot request as too complex to implement (with outcome increasing overall code complexity), so hats off to you guys developing TypeScript and thank you for your work!
(p.s. came here to learn why not - in the end changed my mind and will deal with this._property = ... and will be still happy)
@idchlife The set property goal isn't to save developers one underscore when assigning value. You also might want to add some logic in the set process.
Please reopen this issue. This is clearly a highly desired feature.