Reputation: 1040
Suppose we have following type:
struct MyNullable<T> where T : struct
{
T Value;
public bool HasValue;
public MyNullable(T value)
{
this.Value = value;
this.HasValue = true;
}
public static implicit operator T(MyNullable<T> value)
{
return value.HasValue ? value.Value : default(T);
}
}
And try to compile following code snippet:
MyNullable<int> i1 = new MyNullable<int>(1);
MyNullable<int> i2 = new MyNullable<int>(2);
int i = i1 + i2;
This snipped compiled well and without errors. i1 and i2 casts to integer and addition evaluated.
But if we have following type:
struct Money
{
double Amount;
CurrencyCodes Currency; /*enum CurrencyCode { ... } */
public Money(double amount, CurrencyCodes currency)
{
Amount = amount;
Currency = currency;
}
public static Money operator + (Money x, Money y)
{
if (x.Currency != y.Currency)
// Suppose we implemented method ConvertTo
y = y.ConvertTo(x.Currency);
return new Money(x.Amount + y.Amount, x.Currency);
}
}
Try to compile another code snippet:
MyNullable<Money> m1 =
new MyNullable<Money>(new Money(10, CurrenciesCode.USD));
MyNullable<Money> m2 =
new MyNullable<Money>(new Money(20, CurrenciesCode.USD));
Money m3 = m1 + m2;
And now the question, why compiler generate "error CS0019: Operator '+' cannot be applied to operands of type 'MyNullable<Money>' and 'MyNullable<Money>'"?
Upvotes: 6
Views: 2106
Reputation: 1062600
That is an interesting question... it works with Decimal
, for example, but not TimeSpan
, which are both proper .NET types (unlike float
etc that are primitives) and both have an + operator. Curious!
Of course, you can twist the arm with:
Money m3 = (Money)m1 + (Money)m2;
And it you just use Nullable<T>
it'll work for free, of course - plus you get the compiler + runtime (boxing) support. Is there a reason not to use Nullable<T>
here?
I'll look at the spec; in the interim, you might think about promoting the operator to the MyNullable<T>
; with regular Nullable<T>
, the C# compiler provides "lifted" operators for those supported by the type, but you can't do that yourself. The best you can do is offer all the obvious ones and hope the type supports it ;-p To access operators with generics, see here, available for free download here.
Note you'd probably want to apply the appropriate "lifted" checks - i.e.
x + y => (x.HasValue && y.HasValue)
? new MyNullable<T>(x.Value + y.Value)
: new MyNullable<T>();
Update
The different handling looks to relate to 14.7.4 (ECMA 334 v4) "Addition operator", where it is pre-defined for a range of types including decimal (so that was a bad test by me), since by 14.2.4 (same) "Binary operator overload resolution", the pre-defined operators do get special mention. I don't claim to understand it fully, though.
Upvotes: 10
Reputation: 1500065
Marc is on the right lines - it's section 7.2.4 in the C# 3.0 spec - Binary Operator Overload Resolution.
Basically the steps are:
MyNullable<Money>
.MyNullable<T>
doesn't overload +.MyNullable<int> + MyNullable<int>
this works because of the implicit conversions of each argument to int
- but when we're doing MyNullable<Money> + MyNullable<Money>
it doesn't work because Money + Money
isn't in the set of candidate operators.Upvotes: 9