<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Feb 29, 2016 at 5:13 PM, Mazola Winstrol <span dir="ltr"><<a href="mailto:mazofeifer@gmail.com" target="_blank">mazofeifer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div>I have noticed that, by using generics, we can't overload arithmetic operators.<br><br></div>E.g.:<br><br>...<br>class operator Negative(A: TNullableType<T>): T;<br></div>...<br><br>class operator TNullableType<T>.Negative(A: TNullableType<T>): T;<br></div>begin<br></div><div>    Result := -1 * A;<br></div>end;<br><div><div>....<br><div><br></div><div>wouldn't compile with the error "Operator not applicable to this operand type".<br><br></div><div>Is there any workaround to overcome this?<br></div></div></div></div></blockquote></div><br><div class="gmail_default"><span style="font-family:arial,helvetica,sans-serif">The only solution I have found is to implement ALL operators for TNullableType<T> by defining a generic interface INullableOperatiors<T> that contains methods for all operators, then implement for each type by means of manual interface implementation and then delegate each class operator implementation to the corresponding implementation using RTTI of the generic parameter T </span><span style="font-family:arial,helvetica,sans-serif"><span style="font-family:arial,helvetica,sans-serif">like in Delphi's Generics.Defaults and IComparer<T>/IEqualityComparer<T></span>. This way the implementation of an operator not supported for a specific type (e.g. negation for strings) will raise an exception.<br><br>It's a rather tedious task though and I abandoned it half way and I'm sure Sven would object to such an implementation as it involves manual interfaces. I can send you the unfinished Delphi unit via private email if you want.<br></span></div><br></div></div>