<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body smarttemplateinserted="true">
<div id="smartTemplate4-template">Hi,<br>
<br>
<p> what about overloading operators for OBJECTs?</p>
<p>They do not conflict with any default operators.</p>
<p>I expected this to work, but it did not compile:</p>
<p><br>
</p>
type generic TXQHashset<TKey, TInfo> = object
//(specialize TXQBaseHashmap<TKey,TXQVoid,TInfo>)... <br>
</div>
<div> class operator =(const a, b: TXQHashset): boolean;<br>
end;<br>
<br>
<br>
Cheers,<br>
Benito </div>
<div class="moz-cite-prefix">On 17.04.21 22:12, Jonas Maebe via
fpc-devel wrote:<br>
</div>
<blockquote type="cite"
cite="mid:30c95fd0bfc63b2261559ec0219f1e3c@freepascal.org">On
2021-04-17 22:02, Ryan Joseph via fpc-devel wrote:
<br>
<blockquote type="cite">Since I'm working on generics right now
can we finally, at the very
<br>
least, allow class operators for comparison operators? This is
<br>
literally the only way for a generic class to override the =
operator
<br>
(along with some others) so there's no reason not to allow this.
I
<br>
understand the objection to :=, + etc.. where it returns a copy
of a
<br>
class instance and people could in theory do memory unsafe
things,
<br>
with comparison operators there is no possibility for this. I
already
<br>
made a patch for "advanced records" which is in limbo but It's
trivial
<br>
to adapt this for classes and put restrictions on the type of
<br>
operator.
<br>
</blockquote>
<br>
The issue with allowing it for classes (generic or not) is that
the the = operator already has a meaning for them (pointer
equality). I think in general we don't allow overloading operators
that have a built-in meaning.
<br>
<br>
It would work very well either, because if you'd pass such a class
instance as a TObject parameter and that called routine performs a
comparison, then suddenly the comparison would happen again based
on pointer equality rather than with this custom operator. This
means that e.g. any non-generic container class that uses
comparisons (for sorting or for detecting duplicates) would fail
to work, or at least work differently than generic container
classes. That's something you don't want at all in a programming
language, as it means you constantly have to keep in mind how
something you call was implemented to know how it will behave (the
called routine may even have your class type as parameter type,
but then call something else that uses TObject).
<br>
<br>
<br>
Jonas
<br>
_______________________________________________
<br>
fpc-devel maillist - <a class="moz-txt-link-abbreviated" href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a>
<br>
<a class="moz-txt-link-freetext" href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</a>
<br>
</blockquote>
</body>
</html>