[Issue 1817] New: Creating a lazy tuple function with string literals fails

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 5 06:13:00 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1817

           Summary: Creating a lazy tuple function with string literals
                    fails
           Product: D
           Version: 1.024
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: schveiguy at yahoo.com


I have a function like so, which prints out data only if a flag is true.  This
is used for logging:

void printIfTrue(T ...)(bool flag, char[] formatStr, lazy T)
{
   if(flag)
      writefln(formatStr, T);
}

However, the compiler complains on a line like:
printIfTrue(true, "%s", "hello");

template instance printIfTrue!(char[5u]) error instantiating
testlazyformat.d(34): Error: functions cannot return static array char[5u]

I can understand this because it is trying to replace the type char[5u] with a
delegate that returns char[5u].

However, the compiler could choose to return char[] instead of char[5u] (a
slice), because there is no harm in taking the whole slice of a string literal,
or even a slice of a char[5u] variable.  This is a possible solution to the
problem.

As another solution, lazy evaluation could potentially be replaced with
non-lazy evaluation for simple expressions, that is, expressions that are
determined not to have any operations or effect any changes.  For instance,
lazy evaluation of a string literal could reduce to passing the string literal,
as there is no reason to delay the evaluation for performance, and choosing not
to evaluate it will not affect outside code.  This would also reduce the amount
of code as a delegate that returns the string literal is not generated.


-- 



More information about the Digitalmars-d-bugs mailing list