[fpc-devel] Local classes and interfaces

Sven Barth pascaldragon at googlemail.com
Sat Dec 19 14:51:18 CET 2020


Blaise--- via fpc-devel <fpc-devel at lists.freepascal.org> schrieb am Sa.,
19. Dez. 2020, 12:47:

> Behind the scene, the Closures implementation declares classes and
> interfaces. And, because I strongly believe it to be conceptually the right
> way, such entities are declared in the innermost routine scope. For example,
> -------8<-------
> procedure Foo;
>         type Local = reference to procedure;
> begin
> end;
> -------8<-------
> is essentially
> -------8<-------
> procedure Foo;
>         type Local = interface
>                 procedure Invoke;
>         end;
> begin
> end;
> -------8<-------
>
> FPC does not allow the user to declare local classes/interfaces†, and this
> shall remain so. However, in order to /internally/ support such entities, a
> couple of changes is required. I have those implemented, but, firstly, let
> us establish that the team is on board with the general idea.
>

Considering that it's only intended for internal use, yes I'm aboard with
that.

>
>
> † No idea why, but generic local classes/interfaces are accepted. However,
> FPC instantiates them in the global scope, which results in this wonderful
> bug:
> -------8<-------
> function Foo: TClass;
>         type Cls<T> = class end;
> begin
>         result := Cls<Char>
> end;
>
> function Bar: TClass;
>         type Cls<T> = class end;
> begin
>         result := Cls<Char>
> end;
>
> begin
>         // BUG: 'FALSE'
>         writeln( Foo() <> Bar() )
> end.
> -------8<-------
>

I'd say in this case the bug is that the declaration of those two Cls<>
types is allowed.

By the way: If I remember correctly *specializations* are already placed in
the more nested scope if they use local types (or at least that was the
plan, I think there are still some bugs left there...).

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20201219/daa51410/attachment.htm>


More information about the fpc-devel mailing list