RfC for language feature: rvalue struct

Dukc ajieskola at gmail.com
Mon Jan 30 14:43:21 UTC 2023


On Monday, 30 January 2023 at 14:10:39 UTC, FeepingCreature wrote:
> - we wouldn't have to figure out when we'd need to dup a field 
> on access to avoid mutation-at-a-distance: sSince immutable is 
> transitive, the answer is "never", which also makes the GC happy

This can be accomplished by making return value of the getter 
`const`.

> - we avoid the template overhead of having to generate 
> accessors for every field, with all the required analysis to 
> figure out attributes etc.
> - even struct methods cannot mutate fields, for maximum purity
> - it just looks nicer.

These are all true. However, the proposed feature just feels 
inconsistent with how the rest of the language works. I can't 
pinpoint exactly why, but I think it has to do with that 
Immutability and visibility are usually treated as separate 
issues. This just does not play togeteher with rest of the 
language.

To be fair, it wouldn't be the first feature to be like that. 
`protected` and `lazy` for example also feel out of place IMO.

I feel you use D very differently than I do. I bump to these 
issues rarely enough that I don't feel bothered at all to write 
getters when I do. Plus I consider the struct itself (and rest of 
the module) being able to modify the field mostly a good thing. I 
wonder how people feel in general. If your style is widely 
spread, there might indeed be a case for a language feature.


More information about the Digitalmars-d mailing list