‹ BackHN Continuity

Thread

Why isn't mutable a subtype of immutable, or vice versa?

28 points · 56 comments · ibobev

  1. bruce343434 · · focus · HN ↗
    So really there are 2 orthogonal axes:

    - A: can I change the value

    - B: can something else change the value (can I depend on a predictable stable value)

    Because the axes are orthogonal, hierarchical based subtyping (inheritance) breaks, but type classes (interfaces), ad hoc polymorphism, would work.

    In C, const answers A

    In rust, due to pointer aliasing restrictions (either one mut pointer xor any amount of read only pointers), (lack of) mut answers both A and B

    1. PhilipRoman · · focus · HN ↗
      In C, const can also answer B, but only in non-pointer contexts.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.