* * *

Author Topic: question about the implementation of scoped enumerated types  (Read 462 times)

440bx

  • Jr. Member
  • **
  • Posts: 59
Hello,

I wasn't sure where to post this question.  I hope this is the right place.

I am currently studying FPC's implementation of pascal and it was very nice to see a {$SCOPEDENUMS} available to control the scope of enumerated types. 

This allows two different enumerated types to have elements with the same name since their individual scopes makes them different from each other.  That is rather nice (and logical too :) )

What I find a bit surprising is that once SCOPEENUMS is turned ON then an assignment to a variable of the scoped enum type must explicitly qualify the enumerated element with the name of the type.   Like this:


Code: Pascal  [Select]
  1. type            
  2.   anenum = (bygone, bigtwo, bigthree);
  3.  
  4.   { the following enumeration uses the same names for its elements as the      }
  5.   { previous one.  This is possible only when the scopedenum directive is ON   }
  6.                                                                                  
  7.   otherenum = (bigone, bigtwo, bigthree);                          
  8.                                                                                  
  9. var                                                                              
  10.   A : otherenum = otherenum.bigone;                                              
  11.  

It seems that qualifying the assignment to A is superfluous since A being of type "otherenum" can only take values from that type.

My question is: is there some case, that I have missed, where not qualifying the name of the enum would result in an ambiguity the compiler could not resolve or is the requirement simply "academically clean" ?

Thank you. 

PS: the more I learn about FPC and Lazarus, the more I like them and enjoy them.  Developers, you've really done a superb job, thank you for two excellent tools.

ASerge

  • Hero Member
  • *****
  • Posts: 827
Re: question about the implementation of scoped enumerated types
« Reply #1 on: July 09, 2018, 07:50:24 am »
In my opinion, this is the main purpose of the $SCOPEDENUMS directive. And the fact that it can help to avoid the error of duplication of the identifier, was previously solved by typing definitions in different units, is a side effect.

Thaddy

  • Hero Member
  • *****
  • Posts: 6362
Re: question about the implementation of scoped enumerated types
« Reply #2 on: July 09, 2018, 09:28:09 am »
Note you can mix scoped enums with unscoped.
Code: Pascal  [Select]
  1. {$ifdef fpc}{$mode objfpc}{$H+}{$endif}
  2. type            
  3.   // unscoped
  4.   anenum = (bigone, bigtwo, bigthree);
  5.   {$push} {$scopedenums on}
  6.   // scoped                                                                              
  7.   otherenum = (bigone, bigtwo, bigthree);                          
  8.   {$pop}                                                                              
  9. begin
  10.   writeln(bigone:10,otherenum.bigone);
  11. end.
                                           
I am enjoining wine, not whine....

440bx

  • Jr. Member
  • **
  • Posts: 59
Re: question about the implementation of scoped enumerated types
« Reply #3 on: July 09, 2018, 06:24:36 pm »
Thank you ASerge and Thaddy. 

{$SCOPEDENUMS} is a very nice addition to the language, well worth qualifying the enumeration elements. 

PascalDragon

  • Full Member
  • ***
  • Posts: 130
  • Compiler Developer
Re: question about the implementation of scoped enumerated types
« Reply #4 on: July 11, 2018, 11:21:51 pm »
My question is: is there some case, that I have missed, where not qualifying the name of the enum would result in an ambiguity the compiler could not resolve or is the requirement simply "academically clean" ?

If you have operator overloads for multiple scoped enums or overloads of routines that take different scoped enums as parameters then the compiler would not be able to pick the right one.

440bx

  • Jr. Member
  • **
  • Posts: 59
Re: question about the implementation of scoped enumerated types
« Reply #5 on: July 12, 2018, 12:05:41 am »
Thank you PascalDragon. 

I thought about it some more after I posted the question and realized that the qualifier will be necessary in most cases.  Effectively, a scoped enum is like a record type of constants instead of variables.
 
Just using a field/variable name, which isn't necessarily unique among records and variables, could not identify the record type it belongs to.  The same problem would occur if scoped enums were not qualified.

Thank you for the example you provided, I appreciate your feedback.
« Last Edit: July 12, 2018, 12:13:31 am by 440bx »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus