Re:My personal C coding style as of late 2023
by flakes on Monday October 09, 2023 @02:45AM
(#37817636)
Final only protects the variable from being assigned a new reference (similar to a const pointer). It doesn’t protect any of the underlying data held by the object from being changed, unless the entire hierarchy has every field declared final as well. I still use final heavily in all of my Java code, but it doesnt convey the full intent I would like it to.
[ Parent
| on HN ]
Re:My personal C coding style as of late 2023
by _old_dude_ on Monday October 09, 2023 @03:47AM
(#37818029)
I remember James Gosling saying, a long time ago, that the whole class should be either mutable or not so you do not need to tag some methods with const.
The consequence is that you may define two classes, one non-mutable and one mutable like String/StringBuilder.
Re:My personal C coding style as of late 2023
by layer8 on Monday October 09, 2023 @04:40AM
(#37818329)
It means you have to triplicate each mutable class, because besides the immutable variant you also need the common interface (e.g. CharSequence), in order to pass mutable instances to read-only functions.
Re:My personal C coding style as of late 2023
by paulddraper on Monday October 09, 2023 @05:16PM
(#37825592)
No, there are two classes -- mutable and immutable -- that both implement the immutable interface.
Re:My personal C coding style as of late 2023
by layer8 on Monday October 09, 2023 @06:09PM
(#37826097)
Yes, so three classes. I’m counting a Java interface as a class, because it is the same as a purely abstract class. In any case, three different named types.
As a side note, I would say the interface is unmodifiable, not immutable, because references of the interface type may refer to mutable instances that can mutate while you use it through the interface. Immutable = doesn’t change state, unmodifiable = you can’t change it’s state via that reference (but it might change it’s state due to other concurrent code holding a mutable reference). This nomenclature comes from the “unmodifiable” collection wrappers in Java, which don’t make the underlying object immutable.