logoalt Hacker News

skrebbel11/08/20241 replyview on HN

Only slightly related, I wish there were a way to express this in run-off-the-mill, imperative/eager languages. Eg some way to explicitly mark a class, or something like it, as immutable-but-partially-instantiated. TypeScript supports this a tiny bit by means of its `readonly` keyword which lets you assign a value in the constructor, or in any method called from the constructor. But no later than that! If there was a way to make a field "readonly once_assignable" or something like that, you could do something like in this article (esp if you nest instances of a class that uses this) with an eager language, albeit with a lot more boilerplate (and getters, I assume).


Replies

stonemetal1211/08/2024

Both Java and C# do. In Java marking a variable final makes it immutable after assigned. In C# a variable marked readonly is the same.

show 1 reply