[fpc-pascal] Primitive Record Wrappers

Constantine Yannakopoulos alfasud.ti at gmail.com
Tue Mar 1 14:07:39 CET 2016

On Mon, Feb 29, 2016 at 5:13 PM, Mazola Winstrol <mazofeifer at gmail.com>

> I have noticed that, by using generics, we can't overload arithmetic
> operators.
> E.g.:
> ...
> class operator Negative(A: TNullableType<T>): T;
> ...
> class operator TNullableType<T>.Negative(A: TNullableType<T>): T;
> begin
>     Result := -1 * A;
> end;
> ....
> wouldn't compile with the error "Operator not applicable to this operand
> type".
> Is there any workaround to overcome this?

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 like in Delphi's Generics.Defaults and
IComparer<T>/IEqualityComparer<T>. This way the implementation of an
operator not supported for a specific type (e.g. negation for strings) will
raise an exception.

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160301/053b5248/attachment.html>

More information about the fpc-pascal mailing list