Recent

Author Topic: [SOLVED] Type inheritance  (Read 1661 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Type inheritance
« on: January 31, 2022, 12:55:32 pm »
I have a unit (X) where an enumerated type (TMyEnum) is defined.

I create a component (MyComponent) that uses that unit.

When I create a project that uses MyComponent I need to reference TMyEnum. I can solve this by adding X to the uses clause. This means I have to add X to the uses clause of every project that uses MyComponent.

I don't want to do that.

Is there a better way to inherit TMyEnum?
« Last Edit: January 31, 2022, 03:17:53 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Type inheritance
« Reply #1 on: January 31, 2022, 01:05:13 pm »
Yes, make a separate unit for constants and types you want to include in every other unit.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Type inheritance
« Reply #2 on: January 31, 2022, 01:08:08 pm »
Doesn't that just move the problem to another unit?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Type inheritance
« Reply #3 on: January 31, 2022, 01:09:35 pm »
Another way could be to use includes in the Interface-Section of MyComponent

From the docs (https://www.freepascal.org/docs-html/ref/refsu102.html):
Quote
All identifiers in the interface part of a unit are valid from the point of declaration, until the end of the unit. Furthermore, the identifiers are known in programs or units that have the unit in their uses clause.

Identifiers from indirectly dependent units are not available.
« Last Edit: January 31, 2022, 01:12:44 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Type inheritance
« Reply #4 on: January 31, 2022, 01:28:03 pm »
You can add a same name alias in your unit:
Code: Pascal  [Select][+][-]
  1. // unit A
  2. type TMyEnum = (...);
  3. // Unit B:
  4. type TMyEnum = A.TMyEnum;
  5.  
  6. procedure Foo(value: TMyEnum);

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Type inheritance
« Reply #5 on: January 31, 2022, 01:40:34 pm »
<unit name> dot <enum> Dot like in .
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Type inheritance
« Reply #6 on: January 31, 2022, 01:51:21 pm »
I can't figure this out.

I've attached the project if it helps.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Type inheritance
« Reply #7 on: January 31, 2022, 01:54:43 pm »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Type inheritance
« Reply #8 on: January 31, 2022, 02:31:13 pm »
I have a unit (X) where an enumerated type (TMyEnum) is defined.

I create a component (MyComponent) that uses that unit.

When I create a project that uses MyComponent I need to reference TMyEnum. I can solve this by adding X to the uses clause. This means I have to add X to the uses clause of every project that uses MyComponent.

I don't want to do that.

Is there a better way to inherit TMyEnum?

First, let's see, we have these five statements as facts:
- you have unit X where TMyEnum is declared
- you have a unit where TMyComponent is declared (let's say it's unit Y)
- you have a unit where you use TMyComponet (let's say it's unit Z)
- as TMyComponent needs TMyEnum, you had to include X in uses section of unit Y.
- as you use TMyComponent in unit Z, you had to include Y in uses section of unit Z.

You want to avoid adding X to uses section of unit Z (and any other unit where you use TMyComponent).

The advice you got in posts above says that, in interface section of unit Y, you can declare
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyEnum = X.TMyEnum;
  3.  

Then you will not need to include X in Z to be able to reference TMyEnum.
Okay?
« Last Edit: January 31, 2022, 02:32:55 pm by Zoran »

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Type inheritance
« Reply #9 on: January 31, 2022, 03:16:51 pm »
Got it. Thanks.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018