3 ms·
Kind of a radical idea but IMHO in a high-level language (i.e. not C++ or Rust) the real mistake is structs/classes with mutable fields. None of those problems
by qsort 4y ago
Kind of a radical idea but IMHO in a high-level language (i.e. not C++ or Rust) the real mistake is structs/classes with mutable fields. None of those problems exist in that world.
It's not very ergonomic, but if you start from that assumption you could have ad-hoc syntactic support that desugars to a limited version of an optics library.
- adenozine 4y agoSo, like Erlang? What’s an example of a language that does this by default?
- eru 4y agoCommon Lisp Object System might do something like that?
- qsort 4y agoIdiomatic usages of ML-family languages (Haskell, OCaml) are very close to that.
- packetlost 4y agoRust
- adenozine 4y agoI'd rather gouge my eyes out than work with Rust code. Horrific.
- lysecret 4y agoWe always use dataclasses with frozen=True to make them immutable.
- half0wl 4y agoSame. I treat them as immutable DTOs [0], or "value objects" if you will instead of "just a plain class". An unfortunate drawback is that not everyone is familiar with the concept of immutability, especially in the world of Python. It can also be confusing to explain how "frozen dataclasses are not actually the Python classes you know" :-/ [0] https://en.wikipedia.org/wiki/Data_transfer_object https://en.wikipedia.org/wiki/Data_transfer_object
- dkarl 4y agoThis should be the default, with the fewest keystrokes and the least visual clutter. Mutable state should be the special case that you need extra syntax for.
- packetlost 4y agoIt baffles me that the PEP to add frozendict is still in draft state. It's obnoxious to try and create truly immutable datatypes in Python for... I have no idea why.
- dragonwriter 4y agofrozendict (PEP 416) isn’t in draft, it is rejected frozenmap (PEP 603) is in draft.
- packetlost 4y agoAh right, I'm not sure how they're significantly different. Regardless, not having a immutable dictionary-type is just... frustrating.
- andelink 4y agoI’ve used types.MappingProxyType on occasion for this
- packetlost 4y agoThat's ultimately what I ended up using, but it seems not ideal considering the underlying data is still mutable and you still have to maintain a reference to it somewhere to prevent GC