Delphi – Why does a class need an empty method to call it?

I am evaluating an existing dependency injection library for Delphi, and settled on delphidicontainer due to its simplicity-it only requires a unit file!

However, there is one thing I don’t understand – at the bottom of this example file:

initialization
//with those lines the linked will include the actual code for the services
TCSVDataService.Register;
TPaddedDataService.Register;

These Register methods are empty, but if I comment out these two in the initialization section OK, DIContainer.Get(‘dataservice’) will fail as TDataService.

Why? What are these empty methods?

The Delphi compiler is smart and will try to eliminate unused ones when compiling/linking code Code, so from the perspective of the compiler, the implementation class in the example (for example, TCSVDataService) will not be used anywhere in the program and will be deleted, so these empty method calls are to prevent this from happening. /div>

I am evaluating an existing dependency injection library for Delphi, and settled on delphidicontainer due to its simplicity-it only requires a unit file!

However, there is one thing I don’t understand – at the bottom of this example file:

initialization
//with those lines the linked will include the actual code for the services
TCSVDataService.Register;
TPaddedDataService.Register;

These Register methods are empty, but if I comment out these two in the initialization section OK, DIContainer.Get(‘dataservice’) will fail as TDataService.

Why? What are these empty methods?

The Delphi compiler is smart and will try to eliminate unused code when compiling/linking the code, so from the perspective of the compiler, the example The implementation class in (for example, TCSVDataService) will not be used anywhere in the program and will be deleted, so these empty method calls are to prevent this from happening.

Leave a Comment

Your email address will not be published.