4 ms·
Type safety and encapsulation aren't the same thing. Encapsulation is about hiding implementation details from the user of an API, which is what the comment I
by tgflynn 4y ago
Type safety and encapsulation aren't the same thing. Encapsulation is about hiding implementation details from the user of an API, which is what the comment I originally replied to was claiming you couldn't do in C.
- chrisseaton 4y agoThe void * is (should have been!) an implementation detail, and you're leaking it in the interface - that's not encapsulation. For example if I want to store a __int128 on a 64-bit machine I'll have to deal with stuff like memory allocation and lifetime myself, when the data structure should do that.
- tgflynn 4y agoIt's not an implementation detail, it is part of the interface. The data it points to belongs to the caller and the caller is responsible for managing it, just like anywhere else in C.
- chrisseaton 4y ago> The data it points to belongs to the caller and the caller is responsible for managing it Going back to the start of the thread where you said you didn't understand this: > this means that using, say, a dictionary means that quite a bit of the implementation gets hard coded into every site that uses the dictionary You can see you've just said yourself what you were saying you didn't understand - with data structures in C you're responsible for manually managing things that the data structure would via things like generics in most other languages, with you having to instead hard-code them at the use-site to turn the data into something that can be pointed to and managing the lifetime.