Till
Till

Reputation: 27597

How to declare an enum value as being deprecated in ObjectiveC (2.0)

Suppose a long time ago, I had created the following enumeration:

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricSquare,
   GeometricRectangle,
   GeometricCircle
}GeometricFigures;

I introduced those a while ago within my awesome engine and now I have finally decided that people should not use GeometricSquare anymore as that is covered by GeometricRectangle already.

For a start, I would possibly change my enumeration towards something like this:

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   GeometricSquare = GeometricRectangle,
   GeometricCircle
}GeometricFigures;

That would certainly keep my awesome engine backwards compatible but on the other hand increase the legacy junk. Hence I would like to remove GeometricSquare altogether in a foreseeable future. To make that obvious to the users of my engine, I would like to mark GeometricSquare as being deprecated.

My goal is that the documentation (doxygen) as well as the code completion (Xcode) and last but not least the compiler (GCC) will make it obvious to the user that GeometricSquare should not be used anymore and has been replaced by GeometricRectangle.

For the documentation, I would simply use @deprecated keyword;

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   ///@deprecated Has been replaced by GeometricRectangle
   GeometricSquare = GeometricRectangle,
   GeometricCircle
}GeometricFigures;

But how about Xcode and GCC?

Unfortunately, the usual GCC (method) attribute does not seem to do the job. Adding __attribute__((deprecated)) as drafted below causes a syntax error.

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   GeometricSquare = GeometricRectangle __attribute__((deprecated)),

Parse Issue Expected }

   GeometricCircle
}GeometricFigures;

So obviously that either does not work altogether or I am simply using it wrong.

Upvotes: 22

Views: 5443

Answers (2)

Ben Flynn
Ben Flynn

Reputation: 18922

At a later point a convenience macro was added:

typedef NS_ENUM(NSInteger, GeometricFigures)
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   GeometricSquare __deprecated_enum_msg("Squares are for squares. Use GeometricRectangle.") = GeometricRectangle,
   GeometricCircle,
};

Upvotes: 19

jscs
jscs

Reputation: 64012

I think you're attaching the __attribute__ bit to the wrong side of the assignment operator. This seems to work fine:

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   GeometricSquare __attribute__((deprecated)) = GeometricRectangle,
   GeometricCircle
}GeometricFigures;

and now assigning GeometricSquare gives a compiler warning:

int fig = GeometricSquare;    //'GeometricSquare' is deprecated

Upvotes: 25

Related Questions