Jump to page: 1 2 3
Thread overview
[Issue 3813] New: Bad writeln of string arrays
[Issue 3813] Bad writeln of arrays
Dec 16, 2010
Denis Derman
Jan 28, 2011
Denis Derman
Sep 02, 2011
Kenji Hara
Sep 02, 2011
Kenji Hara
Sep 05, 2011
Kenji Hara
February 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813

           Summary: Bad writeln of string arrays
           Product: D
           Version: 2.040
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-02-18 09:47:14 PST ---
import std.stdio: writeln;
void main() {
    auto a = ["a", "bc"];
    writeln(a);
}

This writeln prints an ugly and misleading output:
a bc
instead of something much more realistic and useful as:
["a", "bc"]


(This is my first bug report in this bug tracker. I will probably add here some more bugs, some of them will probably be duplicates of already present bugs. Most of them will be bugs, I try to limit the true enhancements requests to very few and small, because D2 is now feature frozen. All my bug reports will be relative to D2/Phobos, but many of them can be present in D1 too. If I am doing something wrong please tell me.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@metalanguage.com


--- Comment #1 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-02-18 10:33:23 PST ---
This is by design. The default array bounds were changed from "[" and "]" to "" and "", and the default separator has been changed from ", " to " ".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813



--- Comment #2 from bearophile_hugs@eml.cc 2010-02-18 10:56:31 PST ---
(In reply to comment #1)
> This is by design. The default array bounds were changed from "[" and "]" to "" and "", and the default separator has been changed from ", " to " ".

It's a bad design, much less useful than the design I have shown.
A print like:
["a", "bc"]
Tells you it's probably an array, how long it is, how long its contents are,
and the strings themselves.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813



--- Comment #3 from bearophile_hugs@eml.cc 2010-08-09 07:31:25 PDT ---
In dmd 2.048beta the situation is improved, but it's not good enough yet. This D2 program:


import std.stdio, std.typetuple, std.typecons;
void main() {
    auto a1 = ["10", "20"];
    writeln(a1);
    auto a2 = [10, 20];
    writeln(a2);
    char[] a3 = ['5', '7', '9'];
    writeln(a3);
    auto t1 = TypeTuple!(10, "20", '7');
    writeln(t1);
    auto t2 = tuple(10, "20");
    writeln(t2);
}


Prints:

[10, 20]
[10, 20]
579
10207
Tuple!(int,string)(10, 20)

From that output there is now way, in both the array and the TypeTuple, to tell apart strings, numbers and chars. This doesn't help D2 debugging, and it doesn't help all when you write quick scripts that often use a simple writeln() for their output. In both cases being able to tell apart numbers and strings in the output is quite important.

So my warm suggestion is to put "" around strings, '' around chars when they are printed inside collections (Inside string literals special chars need to be escaped).

So my expected output is:

["10", "20"]
[10, 20]
579
10"20"'7'
Tuple!(int,string)(10, "20")


A possible alternative output:

["10", "20"]
[10, 20]
['5', '7', '9']
10"20"'7'
Tuple!(int, string)(10, "20")



A similar Python 2.7 program:

from collections import namedtuple
a1 = ["10", "20"]
print a1
a2 = [10, 20]
print a2
a3 = ['5', '7', '9']
print a3
t1 = (10, "20", '7')
print t1
t2 = namedtuple('Two', 'a b')(10, "20")
print t2



Prints:

['10', '20']
[10, 20]
['5', '7', '9']
(10, '20', '7')
Two(a=10, b='20')

(In Python strings can be delimited by '' too.)


In D I presume writeln() can't tell that the input is a TypeTuple, so it can't
print something like the () as in Python.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813



--- Comment #4 from bearophile_hugs@eml.cc 2010-08-09 08:41:44 PDT ---
Sorry, the expected results are wrong because writeln() can't see TypeTuple items as inside a collection, so the expected output is:


["10", "20"]
[10, 20]
579
10207
Tuple!(int,string)(10, "20")


And the possible alternative output is:

["10", "20"]
[10, 20]
['5', '7', '9']
10207
Tuple!(int, string)(10, "20")

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813



--- Comment #5 from bearophile_hugs@eml.cc 2010-08-09 08:49:59 PDT ---
See also bug 4605

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 17, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Bad writeln of string       |Bad writeln of arrays
                   |arrays                      |


--- Comment #6 from bearophile_hugs@eml.cc 2010-08-17 05:57:11 PDT ---
See also bug 4660

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 11, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813



--- Comment #7 from bearophile_hugs@eml.cc 2010-10-11 16:20:06 PDT ---
See also bug 5043

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813



--- Comment #8 from bearophile_hugs@eml.cc 2010-10-29 17:28:53 PDT ---
import std.stdio, std.range;
void main() {
    writeln(iota(5));
}


With DMD 2.050 that program prints:

[0, 1, 2, 3, 4]

But that's not an array, it's a lazy sequence, and I'd like to be able to tell apart an array from a lazy sequence in a printout.

A possible simple way to tell them apart is to print that lazy range like this, like an array, but with semicolons instead of commas (in some languages this syntax is used to tell apart linked lists from arrays, but in D lazy ranges are probably more common than lists):

[0; 1; 2; 3; 4]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3813


Denis Derman <denis.spir@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |denis.spir@gmail.com


--- Comment #9 from Denis Derman <denis.spir@gmail.com> 2010-12-16 00:13:28 PST ---
(In reply to comment #4)
> Sorry, the expected results are wrong because writeln() can't see TypeTuple items as inside a collection, so the expected output is:
> 
> 
> ["10", "20"]
> [10, 20]
> 579
> 10207
> Tuple!(int,string)(10, "20")
> 
> 
> And the possible alternative output is:
> 
> ["10", "20"]
> [10, 20]
> ['5', '7', '9']
> 10207
> Tuple!(int, string)(10, "20")

I support this enhancement request.
About char[], I think using a string format rather than an array format is
better, to respect the semantics of "char" (as opposed to ubyte[]). Especially
for debugging: char[] is often used temporarily to manipulate a string, thus we
want to be able to (visually) compare it to a string.

Denis

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2 3