4 ms·
Any language that allows private constructors makes this pretty easy, no?
by kitd 2y ago
Any language that allows private constructors makes this pretty easy, no?
- dgb23 2y agoThe constructor doesn't have to be private, it just has to be a constructor. For example take this library: https://github.com/google/uuid https://github.com/google/uuid It represents UUIDs as `type UUID [16]byte`. It would be trivial to circumvent a constructor: `var uuid UUID`. Go values are zero initialized. (Aside: You might actually have a reason to do this, but often you would rather use https://pkg.go.dev/github.com/google/uuid#NullUUID https://pkg.go.dev/github.com/google/uuid#NullUUID). But it's obvious that you would use one of the constructors in the library to get an actual valid UUID.
- TeMPOraL 2y agoIn C++ it's less obvious, so while a private constructor does most of the job, it's still tricky to come up with a way of designing those types so they feel natural, are easy to construct in the right way (via appropriate parsers), impossible to construct the wrong way by accident, and fulfill all the roles people expect, and don't devolve into impenetrable mess of template metaprogramming that can only be debugged by the author of the type definition framework.