Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private types are visible to everyone?

During program development I accidentally noticed that all types that are declared within a classes has global visibility.

I have always thought that their visibility is restricted to class unless type is referred with class type name like TMyClass.TMytype.Value;

Am I doing something really wrong here, as structures like following:

unit MyTest;

interface

type TMyTest  = class
    constructor Create;

    strict private
        type TMyType = ( NUL, SLEEP );

end;

implementation

// ...

causes conflicts in other units that uses this (MyTest) unit.

If unit has Sleep( 100 ); call, it will conflict with TMyTest.TMyType.SLEEP and prevention of conflicts was why I encapsulated SLEEP inside class and TMyType at the first place.

Any suggestion for a workarounds?

like image 470
Usagi Avatar asked Nov 13 '15 09:11

Usagi


People also ask

Which private variables are visible in subclasses?

Private members are not visible from outside the class. The default visibility allows access by other classes in the package. The protected modifier allows special access permissions for subclasses.

What is meant by private visibility of a method in Java?

Private in Java means the variable or method is only accessible within the class where it is declared.

What are public and private methods?

A public method can be invoked from anywhere—there are no restrictions on its use. A private method is internal to the implementation of a class, and it can only be called by other instance methods of the class (or, as we'll see later, its subclasses).

What is public/private in Java?

Public members can be accessed from the child class of the same package. Private members cannot be accessed from the child class of the same package. Public member can be accessed from non-child class of same package. Private members cannot be accessed from non-child class of same package.


1 Answers

This is actually by design. Your enum values have unit or global scope. They are not private since they are not part of the class. They are scoped at global level.

You can arrange for the enum values to have local scope by including the scoped enums directive:

{$SCOPEDENUMS ON}

The $SCOPEDENUMS directive enables or disables the use of scoped enumerations in Delphi code. More specifically, $SCOPEDENUMS affects only definitions of new enumerations, and only controls the addition of the enumeration's value symbols to the global scope.

In the {$SCOPEDENUMS ON} state, enumerations are scoped, and enum values are not added to the global scope. To specify a member of a scoped enum, you must include the type of the enum.

like image 74
David Heffernan Avatar answered Sep 20 '22 16:09

David Heffernan