You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some case (e.g. to development framework), I'd like to do a destruct operation for the members which is restricted as readonly to release a some large object explicitly. But it would be the compile error if I assign a value to the restricted member in a non-constructor() method.
classA{readonlya: SomeLargeObject;constructor(a: SomeLargeObject){this.a=a;}// explicit destructordestroy(): void{// release this some large object explicitlythis.a=null;// <-- compile error(<any>this).a=null;// <-- no problem(<MutableA>this).a=null// <--- no problem}}interfaceMutableA{a: SomeLargeObject;}
I know we can avoid this error if casting this to any type, but it would not be a type safe. In other approach, we can also do it if we define a "mutable" interface and cast to it, but it would be wordy....
Are there some good approach to remove 'readonly' restriction templorary?
The text was updated successfully, but these errors were encountered:
I'd propose using a public get accessor with a private backing field. There's no way to temporarily remove readonly as that would sort of defeat the purpose of the feature.
I'd propose using a public get accessor with a private backing field.
I also thought the following usecase, so its escape hatch is like a C++'s const_cast<T>()
The public get accessor is defensive from external operation, but it cannot guard from internal operation.
classB{// I'd not like to modify this field only in a constructor/destructorprivateb: SomeLargeObject;constructor(b: SomeLargeObject){this.b=SomeLargeObject;}bar(): void{varblahblah=createSomeLargeObject();this.b=blahblah;// I'd like to ban this operation.}destroy(): void{this.b=null;// I'd like to allow this.}}
environment
[email protected]
description
relate? : #6532
In some case (e.g. to development framework), I'd like to do a destruct operation for the members which is restricted as
readonly
to release a some large object explicitly. But it would be the compile error if I assign a value to the restricted member in a non-constructor()
method.I know we can avoid this error if casting
this
toany
type, but it would not be a type safe. In other approach, we can also do it if we define a "mutable" interface and cast to it, but it would be wordy....Are there some good approach to remove 'readonly' restriction templorary?
The text was updated successfully, but these errors were encountered: