Recent

Author Topic: Params and custom initialization procedures for units initialization  (Read 1741 times)

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Params and custom initialization procedures for units initialization
« Reply #15 on: September 14, 2023, 12:33:27 pm »
In zamtmn's suggestion I see some charm, but not in the way of just invoking a function as he suggested.

The idea (as I see it) is, that you can spezialize the unit like a template. And thus use the unit without any seperate initialization, as you just define the unit with the parameter and directly use that unit. You also don't need to worry about the initialization sequence, as the initialization with the parameter definitely is done before you access code from the unit. But an issue is really the usage of the unit more than once. For the logging purpose one probably wants to use it in more than one unit. Perhaps several units should log to the same file, other units to an other file. Then how to distinguish the function calls to the unit? A call unitlogger.Log('sth') in unit xy should log into 'log1.txt' as initialized, but a call unitlogger.Log('sth') in unit yz should log into 'log2.txt' as initialized there. I don't see an easy way to accomplish that.

An other usecase could be pass defines to the unit, so the unit would be compiled accordingly. Eg:

Code: Pascal  [Select][+][-]
  1. uses
  2.   unitSpezialMath('useTypeExtended');

could pass the define "useExtended" to the unit and could be used there for conditional compilation.
Code: Pascal  [Select][+][-]
  1. {$IFDEF useTypeExtended}

The question would be the same, how to deal with the situation the unit is included in several units.

Many units are pre-compiled, so that would not work.

A unit is in effect a strict singleton with only one flavor defined at it's compile time, not the compile time of those that use it.

 

TinyPortal © 2005-2018