[fpc-pascal] Name collisions in scoped enums
Michael Van Canneyt
michael at freepascal.org
Mon May 4 17:44:16 CEST 2020
On Mon, 4 May 2020, Ryan Joseph via fpc-pascal wrote:
>
>
>> On May 4, 2020, at 2:12 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
>>
>> Methods can't be keywords either, unless prefixed with &.
>>
>> It's not safe to change this, that's why a keyword is a keyword: it supersedes all identifiers, it is part of the language.
>
> Yes but methods can be used without their class prefix so they aren't comparable as identifiers the same way scoped enums are,
> because scoped enums require the prefix to be valid at all. Always, that's fine, I just wanted to make sure this wasn't an omission by accident.
>
> Speaking of that I just remembered something else. In Swift I liked that
> they allowed scoped enums to be used without their prefix in the instance
> that they are assigned to a compatible type, or in the correct context.
>
> For example if you have an enum
>
> TNames = (A, B, C);
>
> and a function
>
> SayNames(names: TNames);
>
> you can call it as SayNames([A, B]); and this is valid because of the context.
>
> Is that possible to implement in scoped enums? It's a nice time saver to not have to deal with the prefix when the context is correct but you still get the namespace protection in the rest of the program.
That is how enums work by default in Pascal.
If you don't force scoped enums, you can still scope them.
if you use
$scopedenums on
then you simply force the use instead of having it optional.
Michael.
More information about the fpc-pascal
mailing list