Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Delphi-News aus aller Welt (https://www.delphipraxis.net/58-delphi-news-aus-aller-welt/)
-   -   Using configuration records and operators to reduce number of overloaded methods (https://www.delphipraxis.net/198511-using-configuration-records-operators-reduce-number-overloaded-methods.html)

DP News-Robot 8. Nov 2018 22:30

Using configuration records and operators to reduce number of overloaded methods
 
When writing libraries you sometimes want to provide users (that is, programmers) with a flexible API. If a specific part of your library can be used in different ways, you may want to provide multiple overloaded methods accepting different combinations of parameters.

For example, IOmniPipeline interface from OmniThreadLibrary implements three overloaded Stage functions.

function* Stage(pipelineStage: TPipelineSimpleStageDelegate;
taskConfig: IOmniTaskConfig = nil): IOmniPipeline; overload;function* Stage(pipelineStage: TPipelineStageDelegate;
taskConfig: IOmniTaskConfig = nil): IOmniPipeline; overload;function* Stage(pipelineStage: TPipelineStageDelegateEx;
taskConfig: IOmniTaskConfig = nil): IOmniPipeline; overload;Delphi’s own System.Threading is even worse. In class TParallel, for example, there are 32 overloads of the &For class function. Thirty two! Not only it is hard to select appropriate function; it is also hard to decode something useful from the code completion tip. Check the image below – can you tell which overloaded version I’m trying to call? Me neither!

https://lh3.googleusercontent.com/-3...55D?imgmax=800

Because of all that, it is usually good to minimize number of overloaded methods. We can do some work by adding default parameters, but sometimes this doesn’t help. Today I’d like to present an alternative solution – configuration records and operator overloading. To simplify things, I’ll present a mostly made-up problem. You can download it from github.



Read more »[SIZE=-2]--- Published under the Creative Commons Attribution 3.0 license[/SIZE]


Weiterlesen...


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:18 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz