4 ms·
I think you're confusing values with types. JS modules can certainly keep a value private, but there's no way for them to expose an opaque type, because that co
by wrs 6mo ago
I think you're confusing values with types. JS modules can certainly keep a value private, but there's no way for them to expose an opaque type, because that concept simply doesn't exist in JS. The language only has a few types, and you don't get to make more of them. TypeScript adds a lot of type mechanism on top, but because it's restricted to being strippable from the actual JS code, it doesn't fundamentally change that.
- hackrmn 6mo agoHere's an opaque type wrapping numbers, in JavaScript: class Age { #value; constructor(value) { if(typeof value != "number") throw new Error("Not a number"); this.#value = value; } }
- wrs 6mo agoThat field is opaque, but the entire type isn’t, no matter what you do. E.g., let x = new Age(); x.notSoOpaque = 42; console.log(x.notSoOpaque); We can all agree to layer conventions on top of the language so we just don’t do stuff that violates the opacity. But the same is true of assembly language.
- hackrmn 6mo agoAssigning to `notSoOpaque` (or any other) property on an object in this case doesn't modify its behaviour, because the property isn't structurally part of the interface -- there's no code defined by the creator / owner of the object (e.g. through the class) that uses it. So it doesn't violate the contract. Private fields are inaccessible, everything else is accessible and is thus part of the interface. I am not saying (and never did) this is the same level as Ada, but your example looks contrived to me -- I don't get the relevance.