> i just played around a little with the c# operator overloading features
> and an idea came to my mind. You all possibly know the Nullable<T>
> Datatype. Now i thought it would be a nice idea to write my own
> Revertable<T> type that maintains the values of multiple assignments and
> can revert to a previous value.
<snip>
> Now the problem is, that the implicit conversion operator creates a
> new instance of Revertable<T> when an assignment happens. So the
> previously assigned value is lost :
Indeed - that's how the assignment operator works, everywhere. The
previous value is lost. Having a type where assignment uses a previous
value feels very counterintuitive to me.
Personally I don't like overloading operators in any but the most
clearcut cases (TimeSpan, DateTime etc) but you might consider
overloading + instead. Then you could do:
Revertable<int> i = 10;
i += 11;
Unfortunately that might *look* like the result should be 21, not a
history of 10, 11...
Jon
Alberto Poblacion - 05 Sep 2007 09:49 GMT
> Revertable<int> i = 10;
> i += 11;
>
> Unfortunately that might *look* like the result should be 21, not a
> history of 10, 11...
Maybe the OP could use some other operator which is less common than +.
For example:
Revertable<string> s = "Hello";
s <<= "Bye";
The <<= would look like you are "pushing" a new value into the stack of
values in s.
Philipp Brune - 05 Sep 2007 10:07 GMT
Jon, Alberto
thank you both for the quick reply :-) I had the idea to "abuse" a
binary operator too, something like
r ^= 12345
and additionaly let Revertable<T> provide a Xor Method if T itself has
an ^ operator defined.
The other way would be to provide a property setter to take
r.V = 12345
but both of them are easy to forget and less comfortable. So i have
to take that my idea (in that special way i wanted it) is impossible to do.
Philipp
Alberto Poblacion schrieb:
>> Revertable<int> i = 10;
>> i += 11;
[quoted text clipped - 9 lines]
> The <<= would look like you are "pushing" a new value into the stack
> of values in s.