Live data from Hacker News

Under the hood: Vec

marma.dev

141–142 of 142 posts

Re: Under the hood: Vec<T>

#141

Earlier quoted context omitted.

> A piece of code that accepts a reference to some type A and only ever reads from it can correctly accept a reference to a subtype of A. The same is true of a piece of code that writes through the reference or returns it. That's how sub-typing works. > A piece of code that accepts a reference to a type A and only ever writes to it can correctly accept a reference to a supertype of A. Have you ever programmed in a la…

> The same is true of a piece of code that writes through the reference or returns it. That's how sub-typing works. But it is not true that it is correctly typed with respect to a a supertype of A (it is not valid to call the code with a reference to a supertype of A). Code that only writes through the reference is correctly typed with respect to a super-type of A (it is valid to call the code with a reference to a s…

> Code that only writes through the reference is correctly typed with respect to a super-type of A (it is valid to call the code with a reference to a supertype of A).

I'm not trying to be snarky, I genuinely want to know what you think of that code snippet I showed you. You claim it should work, but anyone with an understanding of programming would tell you it shouldn't. You can't write to a member variable that doesn't exist. Have you encountered inheritance before? Do you know what a “super-type” is? The mistake you're making here is very basic and I should like to know your level of experience.

Re: Under the hood: Vec<T>

#142
post #25

Earlier quoted context omitted.

https://doc.rust-lang.org/reference/subtyping.html#variance Lifetimes imply the need for the idea of variance.

Rather, any language with both generics and subtyping needs to consider variance, and Rust doesn't have subtyping in general, but lifetimes do technically have subtyping relationships, so lifetimes are the only place where this matters, and fortunately Rust can just infer the right behavior based on some simple type-level heuristics.

Unfortunately, more like. I'd much rather have some explicit syntax for it that potentially never realise it's a feature. I was baffled beyond belief the first time the compiler asked me to put a ghost in my struct.
Post reply on HN