Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 163205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:31:36+00:00 2026-05-11T11:31:36+00:00

When designing libraries, I often end up resorting to the following pattern, which I

  • 0

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.

The basic pattern is:
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)

The library defines the following objects and function:

TThing = Class End;  TThingProcessor = Class Public     Function  CreateThing : TThing; Virtual; Abstract;     Procedure ProcessThing (Thing : TThing); Virtual; Abstract; End;  Procedure DoEverything (Processor : TThingProcessor); 

The calling code then uses the library by overriding the objects and calling DoEverything, as follows –

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; 

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.

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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:31:36+00:00Added an answer on May 11, 2026 at 11:31 am

    Are you using Delphi 2009? This is a great use for generics. Change your declarations to:

    TThingProcessor<T: TThing> = Class Public     Function  CreateThing : T; Virtual; Abstract;     Procedure ProcessThing (Thing : T); Virtual; Abstract; End;   TMyThingProcessor = Class(TThingProcessor<TMyThing>) Public     XSum : Integer;      Function  CreateThing : TMyThing; Override;     Procedure ProcessThing (Thing : TMyThing); Override; End; 

    No more casting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 204k
  • Answers 204k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I ended up using Orca to replace the icons in… May 12, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer try this: SET_TARGET_PROPERTIES(your_executable PROPERTIES LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\" /SUBSYSTEM:WINDOWS") May 12, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer You will have to validate in PHP and then send… May 12, 2026 at 8:41 pm

Related Questions

I have an application that uses a variety of external resources (web services, filesystem,
We are seeing more and more speech recognition implemented and request for libraries that
Does it make sense to group all Interfaces of your Domain Layer (Modules, Models,
I have a large exiting C++ project involving: 4 applications 50+ libraries 20+ third

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.