Is it safe to cast generics in Delphi? -


I need to implement a function that assigns a TDictionary without specifying the exact types. or TDictionary & lt; String, Boolean & gt;

, TDictionary & lt; String & gt; >

Can I declare the function as a parameter with TDictionary:

  function GetMap: TDictionary;  

and then enter the return value:

  type TMyMapType: TDictionary & lt; String, integer & gt; Var MyMap: TMyMapType: Starting ... MyMap: TMyMapType as GetMap; ... End; Edit: It was found that there is no way to declare the 'normal' result parameter type which would be compatible with my three dictionary types  

Looks like I want something like

  type TMyMapType: TDictionary & lt; String ,? & Gt ;;  

The object is not possible in the Pascal language (yet?) In Java it will be something like this:

  Fixed maps & lt; String, integer & gt; GetIntegerMap () {map & lt; String, integer & gt; Results = New TreeMap & lt; String, integer & gt; () {}; Result.put ("foo", Integer.valueOf (42)); Return result; } Fixed map & lt; String ,? & Gt; GetMap () {return gettetegerMap (); } Public static zero main (string [] args) {System.out.println (getMap). Get ("foo")); } "No, there is no" base TDictionary class that descends from all TDictionary versions " 

TDictionary & lt; T, U & gt; 's parent class TEnumerable & lt; TPair & lt; T, U & gt; & Gt; , and its original class is TObject .

It is a little worrisome, but it is necessary to protect type protection, say that you have a TDictionary and lt; String, TMyObject & gt; , and you've passed it to a function that is TDictionary & lt; String, Tubex & gt; You can expect this to work, because you can pass a TBI object in the TOBject parameter. But this is not, and there is a good reason.

The compiler can not check the actual type inside the compiling time function, so stop the routine by taking your dictionary and . Add (own name, self) , where TForm itself (or something else) is not TMyObject and since all the objects are the size of the reference size, it seems to work fine, but when You take it back in your code which knows what the second parameter is supposed to be, then you have a big problem.

There are ways to do the generic work, such as if you expect to get type protection without having to work without any trash - but Delphi does not currently implement it. Delphi prizes, and I'm trying to apply it to the Delphi team in the next release, but we have to see ...


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -