3 ms·
Instead of Java, a comparison should use a language with value types (structs): C#, D, etc
by qznc 11y ago
Instead of Java, a comparison should use a language with value types (structs): C#, D, etc
- sixbrx 11y agoI 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.
- physguy1123 11y agoThis article discussed Java, so I used Java. Also, C#'s value types are somewhat limited aren't they?
- qznc 11y agoI know too little C#. D is very flexible. It also allows to explicitly allocate (emplace) classes on the stack or in an array, for example. You can also wrap structs into a reference counting construct and use them as reference types.