Any ideas for lazy evaluation on varaible argument functions?
Darryl B
darrylbleau at gmail.com
Fri Oct 19 18:54:49 PDT 2007
== Quote from BCS (BCS at pathlink.com)'s article
> also this should work
> t.infoV("{}", expensiveFunction());
> or
> t.infoV("{}, {}, {}", intFn(), floatFn(), charFn());
Hmm, I can't seem to get that to work. Here's what I was using (using Tango):
module TLog;
import tango.io.Stdout;
class TLog
{
bool print = false;
void info(lazy char[] msg)
{
if (print)
Stdout(msg).newline;
}
void infoF(A...)(lazy A a)
{
this.info(this.format(a));
}
private char[] format(lazy char[] fmt, ...)
{
return Stdout.layout().convert(_arguments, _argptr, fmt);
}
}
char[] expensiveFunction(char[] msg)
{
Stdout.format("Expensive! ({})", msg).newline;
return msg;
}
void main()
{
auto t = new TLog;
t.info(expensiveFunction("info"));
t.infoF!(char[], char[])("{}", expensiveFunction("infoF"));
t.print = true;
t.info(expensiveFunction("info2"));
t.infoF!(char[], char[])("{}", expensiveFunction("infoF2"));
//t.infoF("{}", "infoF3");
}
The output I get is as expected:
Expensive! (info2)
info2
Expensive! (infoF2)
infoF2
But if I uncomment that last line as you suggest, I get:
Error: functions cannot return static array char[2u]
Error: functions cannot return static array char[6u]
test.d:15: function TLog.TLog.format (char[],...) does not match parameter types
(int,int)
Error: cannot implicitly convert expression (_param_0()) of type int to char[]
test.d:37: template instance TLog.TLog.infoF!(char[2u],char[6u]) error instantiating
test.d:37: Error: functions cannot return static array char[2u]
test.d:37: Error: functions cannot return static array char[6u]
Which is sort of what I would expect would happen. Am I missing something?
More information about the Digitalmars-d
mailing list