View mode: basic / threaded / horizontal-split · Log in · Help
February 24, 2012
vfprintf equivalent in D
Hi,

Is there an equivalent to vfprintf(const char *fmt, , va_list ap) in D?

--
Thanks,
Sarath
February 24, 2012
Re: vfprintf equivalent in D
On Friday, February 24, 2012 05:22:58 Sarath Kumar wrote:
> Hi,
> 
> Is there an equivalent to vfprintf(const char *fmt, , va_list ap) in D?

If you're dealing with C variadics, then your going to need to use C 
functions. D doesn't just recreate C functions (at least, the standard library 
doesn't). C functions get wrapped when doing so adds some benefit, but with 
something like this, there's no reason to.


If you're using typesafe variadics

void func(int[] arr...)

then you can just pass the array to the next function.


If you're using D-style variadics (which are similar to C variadics but 
include type information), maybe there is, I don't know. Probably. I never use 
them.


However, the typical thing to do in D is to use variadic templates. That's 
what writefln does.

void func(Args...)(Args args)

And with that, you can just pass the arguments on to another function.


http://dlang.org/function.html
http://dlang.org/template.html

- Jonathan M Davis
February 24, 2012
Re: vfprintf equivalent in D
On Thursday, February 23, 2012 21:38:27 Jonathan M Davis wrote:
> On Friday, February 24, 2012 05:22:58 Sarath Kumar wrote:
> > Hi,
> > 
> > Is there an equivalent to vfprintf(const char *fmt, , va_list ap) in D?
> 
> If you're dealing with C variadics, then your going to need to use C
> functions. D doesn't just recreate C functions (at least, the standard
> library doesn't). C functions get wrapped when doing so adds some benefit,
> but with something like this, there's no reason to.
> 
> 
> If you're using typesafe variadics
> 
> void func(int[] arr...)
> 
> then you can just pass the array to the next function.
> 
> 
> If you're using D-style variadics (which are similar to C variadics but
> include type information), maybe there is, I don't know. Probably. I never
> use them.
> 
> 
> However, the typical thing to do in D is to use variadic templates. That's
> what writefln does.
> 
> void func(Args...)(Args args)
> 
> And with that, you can just pass the arguments on to another function.
> 
> 
> http://dlang.org/function.html
> http://dlang.org/template.html

And if your intention was to specifically pass on arguments to writefln, then 
you can just do it, since it's a variadic template.

void myWritefln(Args...)(Args args)
{
   writefln(args);
}

But if you're looking to specifically pass on a C variadic, then use C's 
vfprintf.

- Jonathan M Davis
February 24, 2012
Re: vfprintf equivalent in D
On Fri, Feb 24, 2012 at 05:22:58AM +0000, Sarath Kumar wrote:
> Hi,
> 
> Is there an equivalent to vfprintf(const char *fmt, , va_list ap) in D?
[...]

You don't need one. You can just write:

	void myWritelnWrapper(T...)(T args) {
		...	// insert your own processing here, modify args
			// if you like, etc.
		writeln(args);
	}


T

-- 
Life would be easier if I had the source code. -- YHL
February 24, 2012
Re: vfprintf equivalent in D
Thanks Jonathan and Teoh. I just have to pass the var args in my function to
writefln and Variadic templates is the right solution for me.

--
Thanks,
Sarath
Top | Discussion index | About this forum | D home