When you overload the – unary operators, for an immutable type, you can write it like:
public static Point3 operator - (Point3 p)
{
return new Point3 (-p.X, -p.Y, -p.Z);
}
But for the + unary operator, how should you implement it? Like this:
public static Point3 operator + (Point3 p)
{
return p;
}
or like this:
public static Point3 operator + (Point3 p)
{
return new Point3 (p);
}
Either way is fine. You are not mutating the original object in either of the two methods.
If you call
string.substring(0, string.length()), there is no reason why the original string cannot be returned.The only contract you sign with immutability is that once an object is created, it doesn’t change.