overloading of conversion operators

P

Philipp Brune

Hi all,

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. An Example to make it clear :

Revertable<int> ir0 = 123;
ir0 = 500;
ir0 = 700;
ir0.Revert(2)
int x = ir0;

now x should be equal to 123

This stuff could be used in some kind of simplified in-memory
transactions to help maintain state consitency.

I started up and wrote something like this :

public class Revertable<T>
{
private List<T> valueList = new List<T>();

private Revertable(T value)
{
valueList.Add(value);
}

private T Current
{
get
{
return valueList[valueList.Count - 1];
}
}

public void Revert(int times)
{
...
}

public static implicit operator Revertable<T>(T value)
{
return new Revertable<T>(value);
}

public static implicit operator T(Revertable<T> value)
{
return value.Current;
}
}

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 :

Revertable<int> i = 10;
i = 11; // now i is a new instance of Revertable<int> and the previously
assigned value is lost

Is there a way to convert to a given instance, instead of creating a new
one ? I saw that it is impossible to overload the assignment operator
in c# . I know that i could easily add an Assign(T value) method to my
revertable class but that would make the code much less readable.

Perhaps i have to take another path and take a language that supports
custom assignment operator if one exists ?!

Thanks in advance for your ideas and suggestions

Philipp
 
J

Jon Skeet [C# MVP]

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.

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
 
A

Alberto Poblacion

Jon Skeet said:
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.
 
P

Philipp Brune

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top