<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Am 23.05.2013 12:22, schrieb Maciej
Izak:<br>
</div>
<blockquote
cite="mid:CAFTppY6smo3qoMPOzh_BPYBrNV5wWCoPBD-gJsuTnnpQN_JV+A@mail.gmail.com"
type="cite">
<div dir="ltr"><br>
<div class="gmail_extra"><br>
<br>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Nice.
Now I know where all those bug reports come from :P<br>
</blockquote>
<div><br>
</div>
<div style=""> :D It's my pleasure. Aren't you happy?</div>
</div>
</div>
</div>
</blockquote>
Depends. On the one hand I'm happy that someone stresses the
generics implementation to its limits (like JC Chu does as well),
but on the other hand it means work in - in case of generics - a
bunch of headaches... ;)<br>
<blockquote
cite="mid:CAFTppY6smo3qoMPOzh_BPYBrNV5wWCoPBD-gJsuTnnpQN_JV+A@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">
<div><br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Are
those available in Delphi as well? If not I see no use in
them. </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Consider
this code:<br>
<br>
=== code begin ===<br>
<br>
procedure SomeProc(aValue: Int32);<br>
var<br>
p: Pointer;<br>
begin<br>
p := GetReferenceToValue(aValue);<br>
end;<br>
<br>
=== code end ===<br>
<br>
The value stored in "p" will be of no significant use for
you to be stored in the longterm, because aValue will be
located on the stack and thus the pointer will no longer
be valid once the function exits. This will also be the
case for strings, arrays, records, etc. No compiler magic
will change this.</blockquote>
<div><br>
</div>
<div>Yes, I know that. I need this only for shortterm. In
that case for safety maybe we should remove System.Addr?
;) In Delphi they do that by special interfaces created
for each type (lost of time and memory). First, I can't
implement this by Delphi way because we have still a lot
of generics bugs. Secondly, I think that we can do much
more better implementation of Generics. This is not C#, in
Pascal we have better access to pointers. Now I'm using (I
hope temporarily):</div>
<div><br>
</div>
<div>=== code begin ===<br>
</div>
<div><br>
</div>
<div>
<div> TValueAnsiStringHelper = record helper for
AnsiString</div>
<div> function GetValueSize: Integer; inline;</div>
<div> function GetReferenceToValue: Pointer; inline;</div>
<div> end;</div>
<div><br>
</div>
<div>
<div>function TValueAnsiStringHelper.GetValueSize:
Integer;</div>
<div>begin</div>
<div> Result := Length(Self) * SizeOf(AnsiChar);</div>
<div>end;</div>
<div><br>
</div>
<div>function
TValueAnsiStringHelper.GetReferenceToValue: Pointer;</div>
<div>begin</div>
<div> if Length(Self) <> 0 then</div>
<div> Result := @Self[1]</div>
<div> else</div>
<div> Result := nil;</div>
<div>end;</div>
</div>
<div><br>
</div>
</div>
<div> === code end ===</div>
<div style="">
Etc. for other basic types.</div>
<div style=""><br>
</div>
<div>I, really need this for custom comparers and equality
comparers, for example:</div>
<div><br>
</div>
<div>=== code begin ===<br>
</div>
<div>while AComparer.Compare(AValues[I].GetReferenceToValue,
P.GetReferenceToValue, AValues[I].GetValueSize,
P.GetValueSize) < 0 do<br>
</div>
<div>// ...<br>
</div>
<div>if FEqualityComparer.Equals(AKey.GetReferenceToValue,
LItem.Pair.Key.GetReferenceToValue, AKey.GetValueSize,
LItem.Pair.Key.GetValueSize) then<br>
</div>
<div> === code end ===<br>
</div>
</div>
</div>
</div>
</blockquote>
<br>
I still see no need for this. I would simply do<br>
<br>
=== code begin ===<br>
<br>
if FEqualityComparer.Equals(AKey, LItem.Pair.Key) then<br>
...<br>
<br>
=== code end ===<br>
<br>
Why fall back to pointers for something like this if we can use
static types?! And if that means that the comparer needs to be
implemented for each type, then so be it (you could implement a
default generic comparer, which uses the normal "=" operator, maybe
some others for basic types like strings and a TObject.Equals based
one and the other comparators should be supplied by the user). You
can't do things as equality tests in a general way anyway (best
example: case sensitive vs. case insensitive comparison of strings).
If your concern is performance then you could declare the two
parameters as "constref" so that they are passed by reference and
not by value.<br>
<br>
<blockquote
cite="mid:CAFTppY6smo3qoMPOzh_BPYBrNV5wWCoPBD-gJsuTnnpQN_JV+A@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">
<div>
</div>
<div><br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div>
<blockquote class="gmail_quote" style="margin:0px 0px
0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">measured
by human/programmer<br>
function GetValueSize(Value: T): Integer; // for
string types return Length(s), for Int32 returs 4 etc.<br>
</blockquote>
</div>
You should not store the string's content, but only the
reference to the string. If you use assignment operators
the RTL will take care of the rest.</blockquote>
<div><br>
</div>
<div>But with generics code for some types i don't have
predefined operators (for example: records), and here is
the problem with Generics Dictionary.</div>
</div>
</div>
</div>
</blockquote>
If your implementation of Generics.Dictionary needs certain
operators than the type with which the dictionary is specialized
needs to implement these operators. If the type does not: bad luck.<br>
<blockquote
cite="mid:CAFTppY6smo3qoMPOzh_BPYBrNV5wWCoPBD-gJsuTnnpQN_JV+A@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">
<div>GetValueSize and GetReferenceToValue is in the same
level as System.SizeOf and System.Addr. </div>
<div><br>
</div>
<div>I think it's a natural evolution System.SizeOf and
System.Addr for Generics (to operate on values). </div>
</div>
</div>
</div>
</blockquote>
I see no need for a context sensitive SizeOf and your
GetReferenceToValue is simply flawed, because you can't capture the
correct location of a parameter passed by value as this is just a
copy, no "@Self" will result in the correct value here. E.g.:<br>
<br>
=== code begin ===<br>
<br>
{$mode objfpc}<br>
<br>
type<br>
TLongIntHelper = type helper for LongInt<br>
function GetSelfAddress: Pointer;<br>
end;<br>
<br>
function TLongIntHelper.GetSelfAddress: Pointer;<br>
begin<br>
Result := @Self;<br>
end;<br>
<br>
var<br>
l: LongInt;<br>
<br>
procedure TestProc(aValue: LongInt);<br>
begin<br>
Writeln(hexstr(aValue.GetSelfAddress));<br>
Writeln(hexstr(l.GetSelfAddress));<br>
end;<br>
<br>
begin<br>
l := 42;<br>
Writeln(hexstr(l.GetSelfAddress));<br>
TestProc(l);<br>
end.<br>
<br>
=== code end ===<br>
<br>
=== output begin ===<br>
<br>
0040B000<br>
0140FE4C<br>
0040B000<br>
<br>
=== output end ===<br>
<br>
<blockquote
cite="mid:CAFTppY6smo3qoMPOzh_BPYBrNV5wWCoPBD-gJsuTnnpQN_JV+A@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">
<div>There is no other language as FreePascal and it would
be wrong to introduce something stupid to such an
important system functions. If my thinking is wrong,
please, any hint of an alternative. Without it, I'm
completely stuck.</div>
</div>
</div>
</div>
</blockquote>
<br>
Regards,<br>
Sven<br>
</body>
</html>