Search code examples
pascalfreepascallazarus

Get a constant definition from a specific unit file


I have a complex Lazarus program. One of my sources uses the standard units Graphics and ZStream, both of them contain a definition for clDefault.

The one (in Graphics) is:

const
    clDefault = TColor($20000000);  

The other (in ZStream) is:

type
    Tcompressionlevel=(
      clnone,                     {Do not use compression, just copy data.}
      clfastest,                  {Use fast (but less) compression.}
      cldefault,                  {Use default compression}
      clmax                       {Use maximum compression}
    ); 

In my code when I refer to clDefault I get a compiler error, because I want to use the const definition above to assign it a destination of TColor type, while the compiler tries to use the enumerated constant from ZStream.

How can I instruct the compiler to use the correct definition I want?


Solution

  • Using unit namespaces you can prefix the const clDefault with its unit name:

    Graphics.clDefault
    

    This will let the compiler pinpoint the wanted declaration in this case.

    From Freepascal docs:

    The rules of unit scope imply that an identifier of a unit can be redefined. To have access to an identifier of another unit that was redeclared in the current unit, precede it with that other units name...