* passing flags to a function
@ 2006-05-02 12:25 Dan McMahill
2006-05-02 15:34 ` Ludovic Courtès
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Dan McMahill @ 2006-05-02 12:25 UTC (permalink / raw)
Hello,
I'm sure this is a very basic question about passing "or-able" flags to
a function. In C I might do something like:
#define FLAG_FOO 1
#define FLAG_BAR 2
#define FLAG_BAZ 4
....
myfn(int flags)
{
if (flags & FLAG_BAR)
printf ("bar flag is set\n");
...
}
and then call myfn with things like
myfn(FLAG_FOO | FLAG_BAZ);
now suppose I want to wrap up myfn and make it available from scheme.
What is the normal scheme way for passing in flags to a function?
I can do something like
SCM scm_myfn(SCM flags)
{
myfn (scm_num2int (flags, SCM_ARG1, "myfn"));
return SCM_BOOLEAN_T;
}
but I'm not sure of the best way to define the flags in scheme. Or
maybe this is not "the scheme way".
Thanks for any suggestions.
-Dan
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: passing flags to a function
2006-05-02 12:25 passing flags to a function Dan McMahill
@ 2006-05-02 15:34 ` Ludovic Courtès
2006-05-02 16:21 ` Dan McMahill
2006-05-02 16:14 ` Clinton Ebadi
2006-05-03 1:43 ` Kevin Ryde
2 siblings, 1 reply; 8+ messages in thread
From: Ludovic Courtès @ 2006-05-02 15:34 UTC (permalink / raw)
Cc: guile-user
Hi,
Dan McMahill <mcmahill@mtl.mit.edu> writes:
> I can do something like
>
> SCM scm_myfn(SCM flags)
> {
>
> myfn (scm_num2int (flags, SCM_ARG1, "myfn"));
>
> return SCM_BOOLEAN_T;
>
> }
>
> but I'm not sure of the best way to define the flags in scheme. Or
> maybe this is not "the scheme way".
What you describe above is doable and is an approach sometimes taken.
See for instance the POSIX functions in Guile, e.g., `popen'.
Personally, I prefer to pass a list of symbols rather than a single
number in such situations. This might require some more work if you
need to convert those flags to a C or'ed integer, but not so much
because (i) symbols can be compared with `eq' which is fast, (ii)
the list of flags may usually be small, and (iii) the number of values
that can be taken by the flags is small as well.
SCM_DEFINE (scm_func, "func", 0, 0, 1,
(SCM flags),
"My function takes any number of symbols.")
#define FUNC_NAME "func"
{
int c_flags;
for (c_flags = 0;
scm_is_pair (flags);
flags = SCM_CDR (flags))
{
SCM f = SCM_CAR (flags);
if (scm_is_eq (f, my_first_flag_sym))
c_flags |= MY_FIRST_FLAG;
else if (scm_is_eq (f, my_second_flag_sym))
c_flags |= MY_SECOND_FLAG;
else
scm_wrong_type_arg (FUNC_NAME, 1, f);
}
/* ... */
}
#undef FUNC_NAME
But this is debatable and it also depends on the context.
Thanks,
Ludovic.
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: passing flags to a function
2006-05-02 12:25 passing flags to a function Dan McMahill
2006-05-02 15:34 ` Ludovic Courtès
@ 2006-05-02 16:14 ` Clinton Ebadi
2006-05-03 1:43 ` Kevin Ryde
2 siblings, 0 replies; 8+ messages in thread
From: Clinton Ebadi @ 2006-05-02 16:14 UTC (permalink / raw)
Cc: guile-user
[-- Attachment #1.1: Type: text/plain, Size: 969 bytes --]
On Tue, 2006-05-02 at 08:25 -0400, Dan McMahill wrote:
> Hello,
>
> I'm sure this is a very basic question about passing "or-able" flags to
> a function. In C I might do something like:
...snip...
You can make the flags available to scheme as numbers, and then the
Scheme users can use Guile's bitwise operations (logand, longior,
logxor, etc. [see Simple Data Types -> Numbers -> Bitwise Operations in
the manual]).
> but I'm not sure of the best way to define the flags in scheme. Or
> maybe this is not "the scheme way".
This isn't really the Scheme way, but it works if you want to get
bindings done quickly. Ludovic already recommended the more schemey way,
but this isn't such a bad thing for an extension to a C program.
--
http://unknownlamer.org
AIM:unknownlamer IRC:unknown_lamer@fnode#tpu Jabber:clinton@hcoop.net
I use Free Software because I value freedom over features.
443E 4F1A E213 7C54 A306 E328 7601 A1F0 F403 574B
[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
[-- Attachment #2: Type: text/plain, Size: 140 bytes --]
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: passing flags to a function
2006-05-02 15:34 ` Ludovic Courtès
@ 2006-05-02 16:21 ` Dan McMahill
2006-05-02 16:55 ` Ludovic Courtès
2006-05-07 21:03 ` Marius Vollmer
0 siblings, 2 replies; 8+ messages in thread
From: Dan McMahill @ 2006-05-02 16:21 UTC (permalink / raw)
Cc: guile-user
Ludovic Courtès wrote:
> Hi,
>
> Dan McMahill <mcmahill@mtl.mit.edu> writes:
>
>
>>I can do something like
>>
>>SCM scm_myfn(SCM flags)
>>{
>>
>> myfn (scm_num2int (flags, SCM_ARG1, "myfn"));
>>
>> return SCM_BOOLEAN_T;
>>
>>}
>>
>>but I'm not sure of the best way to define the flags in scheme. Or
>>maybe this is not "the scheme way".
>
>
> What you describe above is doable and is an approach sometimes taken.
> See for instance the POSIX functions in Guile, e.g., `popen'.
>
> Personally, I prefer to pass a list of symbols rather than a single
> number in such situations. This might require some more work if you
> need to convert those flags to a C or'ed integer, but not so much
> because (i) symbols can be compared with `eq' which is fast, (ii)
> the list of flags may usually be small, and (iii) the number of values
> that can be taken by the flags is small as well.
>
Hi Ludovic,
Thanks for the reply. Could you show a short example of what the scheme
function call might look like?
Are you meaning
(foo (list 'flag1 'flag2 'someotherflag))
I don't care much about speed in this particular case.
Thanks
-Dan
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: passing flags to a function
2006-05-02 16:21 ` Dan McMahill
@ 2006-05-02 16:55 ` Ludovic Courtès
2006-05-02 16:56 ` Dan McMahill
2006-05-07 21:03 ` Marius Vollmer
1 sibling, 1 reply; 8+ messages in thread
From: Ludovic Courtès @ 2006-05-02 16:55 UTC (permalink / raw)
Cc: guile-user
Hi,
Dan McMahill <mcmahill@mtl.mit.edu> writes:
> Thanks for the reply. Could you show a short example of what the
> scheme function call might look like?
>
> Are you meaning
>
> (foo (list 'flag1 'flag2 'someotherflag))
Rather, it would look like:
(foo 'flag1 'flag2 'someotherflag)
This is because my example uses "rest" arguments, but you could also do
the same as what you describe using a non-rest list argument.
Thanks,
Ludovic.
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: passing flags to a function
2006-05-02 16:55 ` Ludovic Courtès
@ 2006-05-02 16:56 ` Dan McMahill
0 siblings, 0 replies; 8+ messages in thread
From: Dan McMahill @ 2006-05-02 16:56 UTC (permalink / raw)
Cc: guile-user
Ludovic Courtès wrote:
> Hi,
>
> Dan McMahill <mcmahill@mtl.mit.edu> writes:
>
>
>>Thanks for the reply. Could you show a short example of what the
>>scheme function call might look like?
>>
>>Are you meaning
>>
>>(foo (list 'flag1 'flag2 'someotherflag))
>
>
> Rather, it would look like:
>
> (foo 'flag1 'flag2 'someotherflag)
>
> This is because my example uses "rest" arguments, but you could also do
> the same as what you describe using a non-rest list argument.
>
> Thanks,
> Ludovic.
>
Thanks! I think this should be enough to get me pointed in the right
direction.
-Dan
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: passing flags to a function
2006-05-02 12:25 passing flags to a function Dan McMahill
2006-05-02 15:34 ` Ludovic Courtès
2006-05-02 16:14 ` Clinton Ebadi
@ 2006-05-03 1:43 ` Kevin Ryde
2 siblings, 0 replies; 8+ messages in thread
From: Kevin Ryde @ 2006-05-03 1:43 UTC (permalink / raw)
Cc: guile-user
Dan McMahill <mcmahill@mtl.mit.edu> writes:
>
> SCM scm_myfn(SCM flags)
> {
> myfn (scm_num2int (flags, SCM_ARG1, "myfn"));
Various things in the guile core are done like that, stuff like O_RDWR
for `open'. The list of symbols Ludovic described is done in the
guile-gtk interface and works nicely too.
If you're entirely in scheme then the ice-9 optargs module to take
keywords is good, especially if you want actual values, not just
yes/no flags.
(foo #:error-check #t
#:verbose #f
...)
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: passing flags to a function
2006-05-02 16:21 ` Dan McMahill
2006-05-02 16:55 ` Ludovic Courtès
@ 2006-05-07 21:03 ` Marius Vollmer
1 sibling, 0 replies; 8+ messages in thread
From: Marius Vollmer @ 2006-05-07 21:03 UTC (permalink / raw)
Cc: guile-user
Dan McMahill <mcmahill@mtl.mit.edu> writes:
> Are you meaning
>
> (foo (list 'flag1 'flag2 'someotherflag))
To make this look nicer, you can also write
(foo '(flag1 flag2 someotherflag))
Another option are keyword arguments, like
(foo :flag1 #t :flag2 #t :someotherflag #t)
There is no C-level support for parsing argument lists like this,
unfortunately; you have to do it manually.
--
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3 331E FAF8 226A D5D4 E405
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2006-05-07 21:03 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-02 12:25 passing flags to a function Dan McMahill
2006-05-02 15:34 ` Ludovic Courtès
2006-05-02 16:21 ` Dan McMahill
2006-05-02 16:55 ` Ludovic Courtès
2006-05-02 16:56 ` Dan McMahill
2006-05-07 21:03 ` Marius Vollmer
2006-05-02 16:14 ` Clinton Ebadi
2006-05-03 1:43 ` Kevin Ryde
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).