4 ms·
I like the C#/F# have value types, but they have the downside of not being able to do away with the default constructor for the type, meaning it's harder and le
by sixbrx 11y ago
I like the C#/F# have value types, but they have the downside of not being able to do away with the default constructor for the type, meaning it's harder and less efficient to control type invariants (would have to guard validity in methods repeatedly instead of being able to gaurantee valid construction). For types for which a default/"0" value is a legal value they are good of course.
- qznc 11y agoI don't understand. What is the problem with default constructors?
- sixbrx 11y agoThe problem with the default constructors is just that for value types, you can't not have one. For plenty of kinds of things, there's no sensible default value that would make any sense, but your type must admit such a value anyway, there is no choice. Which means when you have a value of the type, you can't assume it's in a valid state. You have to put checking code somewhere, either in each method of the type, or in functions that receive the type as a parameter, in clients that create objects of the type, etc.