• The whole idea is “make invalid state impossible to represent”. The head element cannot be empty, therefore constructing the type is enough to prove the collection is non empty.

    Doing it your way is possible but requires the programmer who implemented it to have done so in a bug free way.

    It means you have to reallocate + copy the entire vec in the constructor

    IIRC the compilation process can optimize away some of that. Depending on how it is used.

    It would be interesting to see a comparison to see the real world differences. Also there might be implementations with more efficient implementations out there.


    score 1 · canonical

    • Love that the question was “why” and my answer for why got negative downvotes. Sure there alternatives, maybe those alternatives are better. Also…someone asking “why” and getting met with a genuine attempt at an answer should never get downvoted IMHO. Also the downvote is marking this as spam which? I’m not sure what I’m spamming? A paid advertisement for “big type” ??!?!?

      Ahh, this is literally a duplicate comment. That makes sense. Lobsters has been hitting me with anubis failures on my iOS phone when I’m typing by hand and I had to type this one out and submit it several times to get it to work. I didn’t realize some of the others actually went through.


      score 1 · canonical