D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12544 - Differences in ubyte/char enum printing
Summary: Differences in ubyte/char enum printing
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-04-08 03:24 UTC by bearophile_hugs
Modified: 2020-03-21 03:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2014-04-08 03:24:22 UTC
I am not sure if this is a bug, a regression, or it's working as designed. Currently this code shows a difference in the outputs that perhaps should not exist:


void main() {
   import std.stdio;
   enum E1 : ubyte { A='a' }
   E1[10] v1;
   writeln(v1);
   enum E2 : char { A='a' }
   E2[10] v2;
   writeln(v2);
   writefln("%-(%c%)", v2);
}



DMD 2.066alpha output:

[A, A, A, A, A, A, A, A, A, A]
aaaaaaaaaa
aaaaaaaaaa


Expected output:

[A, A, A, A, A, A, A, A, A, A]
[A, A, A, A, A, A, A, A, A, A]
aaaaaaaaaa
Comment 1 monarchdodra 2014-04-08 06:54:41 UTC
The code goes through the "formatRange" branch of `std.format`.

In format range, the code checks if it is a range of chars:
- range of chars => print string (eg: aaaaaaa...)
- range of non-chars => print array (eg: [A, A, A, A, A...

The issue is one of determining "what is a char"?

Currently, the code uses:
is(CharTypeOf!(ElementType!T))

Which mean any type that implicitly casts to char is fair game (including structs with "alias this").

*Arguably*, I think `isSomeChar` would be better, as it only accepts *actual* chars.

But even then, it would still accept enums whose base type is char, as technically, they *are* chars.

Unfortunately, there is always ambiguity when asking to print an enum of a char or string.

I don't know either if this is bug or working as designed. (I don't think it's a regression though... did you test other versions?)
Comment 2 basile-z 2015-11-27 16:04:02 UTC
ok now 2.069