Re: WIP: guc enums

From: "Heikki Linnakangas" <heikki(at)enterprisedb(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Magnus Hagander" <magnus(at)hagander(dot)net>, "pgsql-patches" <pgsql-patches(at)postgresql(dot)org>
Subject: Re: WIP: guc enums
Date: 2008-03-05 13:33:09
Message-ID: 47CEA115.40705@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

Tom Lane wrote:
> "Heikki Linnakangas" <heikki(at)enterprisedb(dot)com> writes:
>> Tom Lane wrote:
>>> What I'd suggest is declaring the actual variable as int. You can still
>>> use an enum typedef to declare the values, and just avert your eyes
>>> when you have to cast the enum to int or vice versa. (This is legal per
>>> C spec, so you won't introduce any portability issues when you do it.)
>
>> That's pretty much the same as int variable and #defined constants. You
>> lose compiler checks, like assigning from one enum type to another, and
>> the "enumeration value ‘FOOBAR’ not handled in switch" warning.
>
> Well, you can at least get the latter if you cast explicitly:
>
> switch ((MyEnum) myvariable) ...
>
> We do this in several places already where the underlying variable isn't
> declared as the enum for one reason or another. Also, local variables
> can be declared as the enum type to get a little more safety.

True, I guess that's not too bad then. Just have to remember to do that.

Regarding the places where we already do that, I could find just three:
src/backend/utils/adt/lockfuncs.c: switch ((LockTagType)
lock->tag.locktag_type)
src/backend/storage/lmgr/lmgr.c: switch ((LockTagType) tag->locktag_type)
src/backend/regex/regc_locale.c: switch ((enum classes) index)

The first and 2nd are really the same, and it seems legitimate. At quick
glance, I couldn't figure out why "index" is an int-variable, and not an
enum, but that code comes from tcl.

> In any case, the alternative being suggested of keeping the variables as
> strings throws away *every* possible code-level advantage of having an
> enum variable classification.

Oh no, I didn't suggest keeping the variables as strings, that's
madness. I suggested keeping the variables as enums, and defining
"setter" functions for them, similar to the assign hooks we have now,
but the setter function wouldn't have to do anything else than assign an
int to the enum variable. The setter function would be just a
replacement for "*((int *)variable) = X".

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

In response to

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2008-03-05 13:40:56 Re: WIP: guc enums
Previous Message Tom Lane 2008-03-05 13:18:19 Re: WIP: guc enums