Hi Sven,<br><br>My code below:<br><br>===============================================<br><span style="font-family:courier new,monospace">program project1;<br>{$mode objfpc}{$H+}<br>{$MODESWITCH advancedrecords}<br><br>uses Classes;<br>
type<br> TMyStringList = record<br> StringList: TStringList;<br> class operator <(s1, s2: TMyStringList): Boolean;<br> end;<br><br>class operator TMyStringList.<(s1, s2: TMyStringList): Boolean;<br>begin<br>
Result := s1.StringList.Count < s2.StringList.Count;<br>end;<br><br>begin<br>end.</span><br>===============================================<br><br>generated these errors:<br><br><span style="font-family:courier new,monospace">project1.lpr(9,20) Error: It is not possible to overload this operator. Related overloadable operators (if any) are:<br>
project1.lpr(9,21) Error: It is not possible to overload this operator. Related overloadable operators (if any) are:<br>project1.lpr(9,53) Error: Impossible operator overload<br>project1.lpr(12,31) Error: method identifier expected<br>
project1.lpr(1,1) Fatal: Compilation aborted<br></span><br>I am using Lazarus 1.0.6/FPC2.6.0-6 on Linux Mint 14/x86_64.<br><br>Sincerely,<br>Shannon<br><br><br><br><div class="gmail_quote">2013/2/26 Sven Barth <span dir="ltr"><<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On <a href="tel:26.02.2013%2009" value="+12602201309" target="_blank">26.02.2013 09</a>:36, Xiangrong Fang wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Sven,<br>
<br>
Could you please give a simple example that shows what you said: require<br>
that the type with which you specialize is a record. Then a class<br>
operator in that record can be defined.<br>
</blockquote>
<br></div>
Let's suppose you have the following generic declaration:<br>
<br>
=== example begin ===<br>
<br>
type<br>
generic TTreap<T1, T2> = class<br>
// let's assume T1 requires "<"<br>
end;<br>
<br>
=== example end ===<br>
<br>
If you use a record to specialize TTreap's T1 parameter then you can do it the following way:<br>
<br>
=== example begin ===<br>
<br>
type<br>
TMyRecord = record<br>
// note: in mode objfpc the modeswitch advancedrecords is needed<br>
class operator < (aLeft, aRight: TMyRecord): Boolean;<br>
end;<br>
<br>
// implementation:<br>
<br>
class operator TMyRecord.<(aLeft, aRight: TMyRecord): Boolean;<br>
begin<br>
// compare aLeft and aRight<br>
end;<br>
<br>
// somewhere else<br>
<br>
TTreapTMyRecordLongInt = specialize TTreap<TMyRecord, LongInt>;<br>
<br>
=== example end ===<br>
<br>
Now the generic will specialize without problem. If you want to specialize other types that don't support class operators then you need to wrap them inside a record, e.g.:<br>
<br>
=== example begin ===<br>
<br>
type<br>
TRecTStringList = record<br>
MyStringList: TStringList;<br>
class operator < (aLeft, aRight: TRecTStringList): Boolean;<br>
end;<br>
<br>
=== example end ===<br>
<br>
You can also use visibility modifiers like private or strict private to hide the MyStringList field and make it accessible only through properties. To simplyfy usage you can also define assignment operators. E.g.:<br>
<br>
=== example begin ===<br>
<br>
type<br>
// maybe it will also work with a TStringList if you use TStrings instead...<br>
TRecTStringList = record<br>
private<br>
fStringList: TStringList;<br>
public<br>
class operator := (aRight: TStringList): TRecTStringList;<br>
class operator := (aRight: TRecStringList): TStringList;<br>
end;<br>
<br>
class operator TRecTStringList.:=(aRight: TStringList): TRecTStringList;<br>
begin<br>
Result.fStringList := aRight;<br>
end;<br>
<br>
class operator TRecTStringList.:=(aRight: TRecStringList): TStringList;<br>
begin<br>
Result := aRight.fStringList;<br>
end;<br>
<br>
// somewhere else:<br>
var<br>
sl: TStringList;<br>
rec: TRecStringList;<br>
begin<br>
// setup sl<br>
rec := sl;<br>
// use rec in the tree<br>
sl := rec;<br>
end;<br>
<br>
=== example end ===<br>
<br>
It's not the nicest solution, but currently the only thing you can do.<div class="HOEnZb"><div class="h5"><br>
<br>
Regards,<br>
Sven<br>
<br>
--<br>
______________________________<u></u>_________________<br>
Lazarus mailing list<br>
<a href="mailto:Lazarus@lists.lazarus.freepascal.org" target="_blank">Lazarus@lists.lazarus.<u></u>freepascal.org</a><br>
<a href="http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus" target="_blank">http://lists.lazarus.<u></u>freepascal.org/mailman/<u></u>listinfo/lazarus</a><br>
</div></div></blockquote></div><br>