On Sat, Sep 20, 2014 at 1:52 PM, Sven Barth <pascaldra...@googlemail.com> wrote:
> It's basically easy, yes, but then one has to deal with code like this: > > === code begin === > > somestrlist.AddObject('Foobar', TObject(42)); > > === code end === > This sort of typecast was usually used to implement associative arrays in Delphi versions that did not have generics. In later versions with generics it is always better to use a TDictionary<string, TObject>, or some other generic associative array class instead. In fact if one expects their code to compile and run with the nextgen compiler they should refrain from using TStringList like that altogether or they are certain to get AVs and leaks in mobile devices -has happened to me. The only problem would arise where this technique has been used with TStrings properties in classes one has no control over (e.g. TListBox.Items) that cannot be converted into TDictionary<string, TObject> but this could be solved with a generic boxing class: type TBox<T> = class private FItem: T: public constructor Create(const AItem: T); property Item: T read FItem write FItem; operator Implicit(const ABox: TBox<T>): T; // Result := ABox.FItem; operator Implicit(const AItem: T): TBox<T>; // Result := TBox<T>.Create(AItem); end; ... MyListBox.Items.Add('someint', TBox<Integer>.Create(42)); MyInteger := TBox<Integer>(MyListBox.Items.Objects['someint']); // Safe ObjRef to ObjRef hardcast, then operator overload In a refcounted version of Object Pascal it might make sense to prohibit casting from a bare pointer or ordinal data type to TObject and vice-versa, thus turning object pointers into object reference variables, like in languages with garbage collection. This would be of great help to a person who is converting legacy code, and it is something I miss in Delphi. In cases where an object reference does indeed need to be turned into or from a non refcounted variable a helper generic class could be used to perform the typecast, something like: ANativeInt := TObjRefMarshal<TStream>.MarshalTo<NativeInt>(AStream); AStream := TObjRefMarshal<TStream>.UnmarshalFrom<NativeInt>(ANativeInt); Regarding TObject.Free, in Delphi nextgen it is a nop and any calls to it are optimized away. The object will be destroyed when the variable "o" goes out of scope and its one and only reference is released. --Constantine
_______________________________________________ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel