2 ms·
I keep forgetting the reasoning for this part of the design - why is the value semantics baked in the declaration site and not at the use site? Why does the cl
by swaranga 2mo ago
I keep forgetting the reasoning for this part of the design - why is the value semantics baked in the declaration site and not at the use site?
Why does the class author have to declare it as a value type? I know this has been rehashed before many times by Brian but I keep forgetting the reasoning.
- cogman10 2mo agoCall site is tricky because you need safety guarantees about the underlying data for it to be a value type. For example, it needs to be immutable. If you were to call a function with a site declared value type, the function you call would have to have syntax as well to indicate "this is a value type I'm receiving and not a regular type" so that it doesn't do an illegal operation with the value type.
- DarkNova6 2mo agoI think it keeps going back to "preserving type invariants". Turning an object into a value can have surprising constraints on the integrity of the type. These include: forced empty constructor (for bulk initialiation in arrays), violation of cross-field invariance (think of a Range-type where "end" value is suddenly smaller than the "start" due to a data race), etc. Java always had a focus on enabling library authors over empowering call-site users. And I think approach has proven its dividend.