Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

delphi - Why aren't local type declarations inherited?

Let's say I have a class that declares a local type like this:

type
  TAncestor = class
    type
      TLocalType = (just, some, example); 
  end;

Then, I want to use that local type in a subclass. However, the following doesn't compile.

type
  TChild = class (TAncestor)
    procedure Test (AVariable: TLocalType);  // error: undeclared identifier: 'TLocalType'
  end;

In order to use the type, it seems a fully qualified type name is needed. The following compiles:

type
  TChild = class (TAncestor)
    procedure Test (AVariable: TAncestor.TLocalType);
  end;

While this works, it feels rather unelegant. (Also, if I ever want to change the ancestor of TChild, I'd have to change the class name in both lines.) I'm probably missing something; what's the reason for the type declaration not simply being inherited from the ancestor?

question from:https://stackoverflow.com/questions/65860291/why-arent-local-type-declarations-inherited

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The type is actually inherited. You can access it by qualifying the identifier with the child class in addition to the ancestor class:

You can also use TChild.TLocalType:

TAncestor = class
type
    TLocalType = (just, some, example);
end;

TChild = class (TAncestor)
    procedure Test (AVariable: TChild.TLocalType);
end;

Why is it like that? Because of a design choice at Embarcadero.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...