<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Sep 20, 2014 at 1:52 PM, Sven Barth <span dir="ltr"><<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div id=":2j7" class="" style="overflow:hidden">It's basically easy, yes, but then one has to deal with code like this:<br>
<br>
=== code begin ===<br>
<br>
somestrlist.AddObject('Foobar'<u></u>, TObject(42));<br>
<br>
=== code end ===</div></blockquote></div><br><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">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.<br><br>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:<br><br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">type<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> TBox<T> = class<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> private<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> FItem: T:<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> public<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> constructor Create(const AItem: T);<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> property Item: T read FItem write FItem;<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> operator Implicit(const ABox: TBox<T>): T; // Result := ABox.FItem;<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> operator Implicit(const AItem: T): TBox<T>; // Result := TBox<T>.Create(AItem);</div> end;<br><br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">...<br><br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> MyListBox.Items.Add('someint', TBox<Integer>.Create(42));<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> MyInteger := TBox<Integer>(MyListBox.Items.Objects['someint']); // Safe ObjRef to ObjRef hardcast, then operator overload<br><br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">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:<br><br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> ANativeInt := TObjRefMarshal<TStream>.MarshalTo<NativeInt>(AStream);<br></div><div class="gmail_default" style="font-family:trebuchet ms,sans-serif"> AStream := TObjRefMarshal<TStream>.UnmarshalFrom<NativeInt>(ANativeInt);</div><br><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">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.<br></div><br>--Constantine<br></div></div>