Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding casting when passing objects through library code (In Delphi)
    primarykey
    data
    text
    <p>When designing libraries, I often end up resorting to the following pattern, which I don't like as it results in lots of type-casting.<br></p> <p>The basic pattern is:<br> The code using the library hands an object to the library, the library then hands the object back to the calling code. The calling code is forced to cast the object, as the library hands back a generic type. (Stripped-down code example below)</p> <p>The library defines the following objects and function:</p> <pre><code>TThing = Class End; TThingProcessor = Class Public Function CreateThing : TThing; Virtual; Abstract; Procedure ProcessThing (Thing : TThing); Virtual; Abstract; End; Procedure DoEverything (Processor : TThingProcessor); </code></pre> <p>The calling code then uses the library by overriding the objects and calling DoEverything, as follows -</p> <pre><code>TMyThing = Class(TThing) Public X : Integer; End; TMyThingProcessor = Class(TThingProcessor) Public XSum : Integer; Function CreateThing : TThing; Override; Procedure ProcessThing (Thing : TThing); Override; End; Function TMyThingProcessor.CreateThing : TThing; Begin Result := TMyThing.Create; End; Procedure TMyThingProcessor.ProcessThing (Thing : TThing); Begin XSum := XSum + (Thing As TMyThing).X; //Here is the problem, the caller is forced to cast to do anything End; </code></pre> <p>The processor class is also a TThing factory. The library guarantees that it will only pass TThings to the corresponding TThingProcessor that created them, so it works, but isn't type-safe. While the code above is a bit stupid in that it doesn't really do anything, it shows why ProcessThing can't simply be shifted to TThing and be polymorphic - the XSum variable needs to be updated.</p> <p>How can I restructure the code so the cast is unnecessary? I need to keep the library code separate but be able to accept any type.</p> <p>Edit: Changed the hard-cast to an as-cast due to suggestion so it will at least throw exception instead of crash in the case of mismatched types</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload