[fpc-devel] Library announcement: Generics.Collections
Maciej Izak
hnb.code at gmail.com
Thu May 23 12:22:37 CEST 2013
Nice. Now I know where all those bug reports come from :P
>
:D It's my pleasure. Aren't you happy?
Are those available in Delphi as well? If not I see no use in them.
Consider this code:
>
> === code begin ===
>
> procedure SomeProc(aValue: Int32);
> var
> p: Pointer;
> begin
> p := GetReferenceToValue(aValue);
> end;
>
> === code end ===
>
> 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.
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):
=== code begin ===
TValueAnsiStringHelper = record helper for AnsiString
function GetValueSize: Integer; inline;
function GetReferenceToValue: Pointer; inline;
end;
function TValueAnsiStringHelper.GetValueSize: Integer;
begin
Result := Length(Self) * SizeOf(AnsiChar);
end;
function TValueAnsiStringHelper.GetReferenceToValue: Pointer;
begin
if Length(Self) <> 0 then
Result := @Self[1]
else
Result := nil;
end;
=== code end ===
Etc. for other basic types.
I, really need this for custom comparers and equality comparers, for
example:
=== code begin ===
while AComparer.Compare(AValues[I].GetReferenceToValue,
P.GetReferenceToValue, AValues[I].GetValueSize, P.GetValueSize) < 0 do
// ...
if FEqualityComparer.Equals(AKey.GetReferenceToValue,
LItem.Pair.Key.GetReferenceToValue, AKey.GetValueSize,
LItem.Pair.Key.GetValueSize) then
=== code end ===
measured by human/programmer
>> function GetValueSize(Value: T): Integer; // for string types return
>> Length(s), for Int32 returs 4 etc.
>>
> 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.
But with generics code for some types i don't have predefined operators
(for example: records), and here is the problem with Generics Dictionary.
GetValueSize and GetReferenceToValue is in the same level as System.SizeOf
and System.Addr.
I think it's a natural evolution System.SizeOf and System.Addr for Generics
(to operate on values). 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.
> http://bugs.freepascal.org/**view.php?id=24283<http://bugs.freepascal.org/view.php?id=24283>(CRITICAL! Very Important!)
>>
> I don't consider nested specializations as critical. It's likely that I
> will fix this after I've fixed other generic problems...
>
As critical error I mean - hardcore incompatibility with Delphi version of
Generics.Collections. With critical error you can't compile Delphi code "as
is" with this library.
--
Regards,
HNB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20130523/20c23bd4/attachment.html>
More information about the fpc-devel
mailing list