scopepascalfreepascal

Completely import a module into current scope


Say I have 2 Units, MainUnit, and ExampleClass.

MainUnit:

Unit MainUnit;

interface

Uses ExampleClass;

function ReturnFive: Integer;

implementation

function ReturnFive: Integer;
begin
  ReturnFive := 5;
end;

begin
end.

ExampleClass:

Unit ExampleClass;

{$mode objfpc}

interface

type 
  ClassThing = Class
    SampleValue: Integer;
  end;
  

implementation

begin
end.

Now, I'd like to only import MainUnit, but still be able to use ClassThing. MainUnit uses ExampleClass, but ClassThing isn't usable when you import MainUnit.

I don't really want to just use ExampleClass along with MainUnit, I'd prefer to keep it in one uses statement.

How do you do this?


Solution

  • put

    type ClassThing = ExampleCLass.ClassThing;
    

    in the interface of mainunit.

    The principle also works for consts, but only "real" ones (not typed ones which are more initialized vars):

    const myconst = unitname.myconst;
    

    Nearly all my much used types are similar aliases, so that I can easily move around where they are defined without changing the uses clause in all the businesscode units