www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1817] New: Creating a lazy tuple function with string literals fails

reply d-bugmail puremagic.com writes:

           Summary: Creating a lazy tuple function with string literals
           Product: D
           Version: 1.024
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: schveiguy 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)
      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

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.

Feb 05 2008
parent d-bugmail puremagic.com writes:

yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
           Keywords|                            |rejects-valid
                 CC|                            |yebblies gmail.com
           Platform|x86                         |All
            Version|1.024                       |D1
            Summary|Creating a lazy tuple       |(D1 only) Lazy parameters
                   |function with string        |cannot accept strings
                   |literals fails              |
           Severity|normal                      |enhancement

Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2012