relax disabled Final!T unary operators

Q. Schroll via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 23 16:30:43 PDT 2017


In std.experimental.typecons.Final the operators ++ and -- are 
disabled. I suspect, this was done with simple types as int in 
mind, where increment is nothing different from += 1, which is by 
definition an assignment. From the distant standpoint, there is 
no reason to disable them at all. They are modifying the object, 
but calling a modifying method on a Final! struct/class is not 
disabled either. An operation need not be disabled because it is 
equivalent to an assignment.
I agree that this behavior is natural. Final!int should not be 
modifiable at all, that's precisely what immutable int does. This 
leads to the following conclusion:
Making Final an alias to immutable for types without 
indirections. They cast implicitly to mutable. E.g. Being allowed 
to assign the components of Tuple!(int, int), but not the tuple 
itself is ridiculous because that's the same.
My conclusion: Final only makes sense for things that have 
indirections.

The things are handled similarly for overloading opAssign for 
classes. Identity assignment is disallowed to overload, but not 
other forms, because other forms are just modifying operations. 
The reason why any form of assignment should be disallowed is 
simple: It is confusing and we would have to determine which 
assignments to allow. The Final implementation cannot know which 
assignment to a struct behaves like modifying. Allowing 
non-identity class assignment is an option, but I'm against it: 
It's still an assignment by definition.


More information about the Digitalmars-d mailing list