Any ideas for lazy evaluation on varaible argument functions?

Daniel Keep daniel.keep.lists at gmail.com
Fri Oct 19 19:37:58 PDT 2007


Darryl B wrote:
> ...
> 
> 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?

Aaah.  It's those bloody static arrays again!

Try this instead of that last line: t.infoF("{}"[], "infoF3"[]);

One of the absolute banes of template metaprogramming in D are
statically sized arrays.  For instance, the type of "{}" is *not*
char[]; it's char[2u].

This is a problem for you because what "lazy" does, is it converts each
argument into a delegate that returns that type.  So in effect, what
that call looks like on the inside is:

void infoF(char[2u] delegate() a0, char[6u] delegate() a1)
{
    ...
}

In D, you *cannot* return statically sized arrays from functions.  So
the compiler borks.

Sadly, I don't think there's any way to fix this.  The problem is that
if you've got IFTI (which is what allows you to omit the explicit
template instantiation), the arguments can't be very complex.  What you
would need is something like this:

void infoF(A...)(lazy DecayStaticArrays!(A) a) { ... }

Where "DecayStaticArrays" turns all statically-sized arrays into
dynamically-sized ones: so (char[2u], char[6u]) would become (char[],
char[]).

So yeah: you *can* do this, you just need to put [] after any
statically-sized array variables or array literals.

	-- Daniel



More information about the Digitalmars-d mailing list