Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Wrapping a C function that takes a variable number of arguments
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-09-07 (02:34)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Wrapping a C function that takes a variable number of arguments
On Fri, 2007-09-07 at 00:24 +0100, Joel Reymont wrote:
> I'm trying to figure out how to wrap Cocoa's NSLog [1].
> It's basically just like fprintf in that the signature looks like this
> void NSLog(NSString *format, ...);
> There must be an idiomatic way of creating a wrapper for such a  
> function, probably with a bit of dark magic. Would someone please  
> show me how?

Variants. In Caml you pass

	NSLog(fmt, [Int 1; Double 2.0])

The wrapper can then decode the 'fmt' and check that the corresonding
variant has a suitable type.

It is not possible, however, to call the C function in a conforming way.
Instead you have to cheat. If there is a 

	vNSLog(fmt, va_args)

function you're cool, the cheat is easy. If you're using gcc as your
C compiler, read up on 


and how gcc implements varargs.

The best cheat is this one: scan the fmt, and break it into
separate %codes like:

	"Hello %s today %d\n"

will be (say) Str.split on % to give

	"Hello "
	"s today "

Now call NSLog 3 times, adding the % back to the second and third

	NSLog("Hello ");
	NSLog("%s today ",vs);

in a loop, obviously, which sets vs if the variant is a string,
vi if it is an integer, vd if it is a double, etc etc.

Note this will NOT work if NSLog isn't functorial, that is:

	NSLog (x ^ y) = NSLog (x); NSlog (y)

eg if it adds an 'end of line' or 'date and time stamp' to the
log message, then this technique won't work so well.

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: