The list below is based on a G+ discussion in a single language, but has way broader aspects.
It’s on value types, mutability, parameterless constructors and expectations of compiled code.
I’ve bitten myself in the foot with mutable types in too many languages too often, so I started advocating this years ago at clients, and now in this blog-post.
TL;DR:
- some languages disallow parameterless constructors: C++, C# and Delphi are examples
- this historically stems from the C++ and C# background
- it has to do with them not wanting to automatically call them upon array initialisation taking a lot of CPU time
- most languages do not stop you from making mutable value types, but in practice your value types should be immutable as otherwise you will open a can of worms, for instance you will have a hard time:
- preventing threading issues
- making code following functional patterns
- scaling your code by making your algorithms supporting parallel execution
- parameterless constructors include constructors with parameters having default values
Some links that explain this in more depth:
- [WayBack] c# – const, readonly and mutable value types – Stack Overflow
- [WayBack] c# – Why can’t I define a default constructor for a struct in .NET? – Stack Overflow (Thanks Jon Skeet)
- [WayBack] c# – const, readonly and mutable value types – Stack Overflow (thanks vc 74)
- [WayBack] Parameterless constructors on record types: I wanted to have a constructor on a record type that would set default values for the elements… – Girish Patil – Google+
- [WayBack] c# – Why are mutable structs “evil”? – Stack Overflow
- [WayBack] delphi – Why are parameterless constructors not allowed on records? – Stack Overflow
- [WayBack] Delphi instance methods and value types – twm’s blog via [WayBack] David Heffernan commented on Girish Patil’s post on G+ But you should never make instance methods on value types that mutate the value. Otherwise you … – Thomas Mueller (dummzeuch) – Google+ (this is what started me to collect this overview)
The “just pass them as reference” often seen as reason to explain “mutable value types are OK” is exactly describing why they are not OK.
–jeroen