From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,88f2e1f9c46b583 X-Google-Attributes: gid103376,public From: adam@irvine.com (Adam Beneschan) Subject: Re: Flags and Ada Date: 1997/02/07 Message-ID: <5dgd14$mo@krusty.irvine.com>#1/1 X-Deja-AN: 215188729 references: <5dbqaj$uq8$1@A-abe.resnet.ucsb.edu> organization: /z/news/newsctl/organization newsgroups: comp.lang.ada Date: 1997-02-07T00:00:00+00:00 List-Id: Graham Hughes writes: >As a getting-used-to-Ada project, I'm trying to translate a regular >expression library from C. I'm wondering what the idiomatic Ada way to >say some of the things the C library uses; specifically, it does things >like > > regcomp(rx, yadda yadda, RX_FOO | RX_BAR); > >How would I do this with Ada? Should I just have a whole bunch of >booleans in the function prototype defaulting to whatever the default >should be? In addition to solutions posted by others, here's one method I like. It's not the most efficient, but I like the readability I get at the calling side. type Regcomp_Option is (RX_FOO, RX_BAR, RX_BAZ, RX_SHEESH, RX_WHATEVER, ...) type Regcomp_Option_Array is array (natural range <>) of Regcomp_Option; No_Options : constant Regcomp_Option_Array := (1 .. 0 => RX_FOO); -- this is a zero-length array procedure Regcomp (...); procedure Regcomp (..., Option : in Regcomp_Option); procedure Regcomp (..., Options : in Regcomp_Option_Array); procedure Regcomp (...) is begin Regcomp (..., No_Options); -- calls the array version -- of Regcomp end Regcomp; procedure Regcomp (..., Option : in Regcomp_Option) is begin Regcomp (..., (1 => Option)); -- calls the array version -- of Regcomp end Regcomp; In the body of Regcomp, you'd probably have an array type like: type Regcomp_Option_Set is array (Regcomp_Option) of boolean; and start with Option_Set : Regcomp_Option_Set; ... Option_Set := (others => false); for I in Options'range loop Option_Set (Options (I)) := true; end loop; so that you can refer to Option_Set(RX_FOO), Option_Set(RX_BAR), etc., in the procedure body. Then, when calling Regcomp, you can say something like Regcomp (Rx, Yadda_Yadda, (RX_FOO, RX_BAR)); -- array of options Regcomp (Rx, Yadda_Yadda, RX_BAZ); -- just one option Regcomp (Rx, Yadda_Yadda); -- no options -- Adam