CTFE writeln again (__ctfeWriteln)

bearophile bearophileHUGS at lycos.com
Mon Aug 8 12:19:43 PDT 2011


KennyTM~:

> I understand that you could implement `writeln` in terms of `write`, but 
> I question its practical value.   Why do you need to print a tree at 
> compile time?  pragma(msg) and __ctfeWriteln are mainly for debugging, 
> and adding a newline is very suitable (e.g. it won't mess up the error 
> message in the next line).

What if you are debugging a program (as I have written) that uses trees? I'd like the freedom to print textual trees one piece at a time (in Python I have written two or three small programs that print trees textually). It's not just trees, it's also other 2D tables. If you add the newline then I am forced to print one table line at a time, I can't print its items one after the other.

Even if printing at compile-time is mainly for debugging, if you add that newline you make other unexpected future usages harder. You do not know how D will be used ten years from now.

In Python2 there the main printing statement adds a space between printed items. This is quite handy. But what if you do not want that space? Things gets harder. They have had to wait for Python3 to fix this design mistake.

The moral of the Python2 story is that you are allowed to add a handy printing function (that adds newlines and spaces) only if you _also_ have a function that prints nude strings (or nude chars). But if you have only one function, then it has to be a function that doesn't add extra things that you can't remove.

(An alternative design, if you want a single function is a function templated on a boolean __ctfeWrite!(bool newline=true)(...). I don't know how much nice this is).

Bye,
bearophile


More information about the Digitalmars-d mailing list