Jump to page: 1 2
Thread overview
[Issue 14998] Cannot put a char into a char[]
Nov 08, 2015
Jack Stouffer
Dec 01, 2015
bb.temp@gmx.com
Jan 17, 2018
Jack Stouffer
Jan 17, 2018
Jack Stouffer
Jan 17, 2018
Jack Stouffer
Mar 21, 2020
Basile-z
November 08, 2015
https://issues.dlang.org/show_bug.cgi?id=14998

Jack Stouffer <jack@jackstouffer.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jack@jackstouffer.com
           Severity|enhancement                 |normal

--- Comment #1 from Jack Stouffer <jack@jackstouffer.com> ---
Changing from enhancement to normal as this seems like a bug and not an enhancement request.

--
December 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14998

bb.temp@gmx.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bb.temp@gmx.com

--- Comment #2 from bb.temp@gmx.com ---
(In reply to schneider.cedric from comment #0)
> Following code does not compile:
> 
> int main()
> {
>      import std.stdio;
>      import std.range;
>      char[] c;
>      c ~= 'a';
>      c ~= 'b';
>      c.put('c');
>      writeln(c);
>      return 0;
> }
> 
> The error is:
> 
> C:\dmd\src\phobos\std\range.d(9,9): Error: static assert  "Cannot put a
> char into a char[]." (Test)

What did you expect as result ? put() is ***poorly designed***. The ddoc says itself that "well, depending on the input and the output...".

There is no constraint in put() and it uses internal checks such as "static if
(is(template!Stuff))" that automatically fail to report the right error line if
the error comes from another template such as the private "putChar()".

So in short:
- look at the result when using another type.
- tell if it's worth implementing it for char in char[]
- close.

--
December 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14998

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy@yahoo.com
         Resolution|---                         |WONTFIX

--- Comment #3 from Steven Schveighoffer <schveiguy@yahoo.com> ---
a char[] is not a range of char but rather a range of dchar.

Phobos refuses to put into an auto-decode range (the 'front' result of a char[] is not ref, so cannot be written to).

The solution is to use byCodeUnit:

import std.utf;
auto r = c.byCodeUnit;
r.put('c');

I think there is general agreement that auto-decode strings were a mistake, but until that decision is changed, this can't be fixed.

--
December 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14998

--- Comment #4 from schneider.cedric@gmx.de ---
If the error message had read "Cannot put a char into a char[] because it is a range of dchar" this issue would not exist.

So, proper resolution of this issue would be improving the error message, as "cannot put a char into a char[]" is somewhat confusing, especially if you're less familiar with the language (like me).

--
December 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14998

--- Comment #5 from Steven Schveighoffer <schveiguy@yahoo.com> ---
(In reply to schneider.cedric from comment #4)
> If the error message had read "Cannot put a char into a char[] because it is a range of dchar" this issue would not exist.
> 
> So, proper resolution of this issue would be improving the error message

It would need to be a special case, because the error message text is generically produced from the type names.

I somewhat disagree that a better error message would dissuade someone from believing it should be possible. Common sense ("of course I can put a char inside a char[]! Watch this: buf[0] = 'c';") trumps the best worded error messages.

--
December 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14998

--- Comment #6 from schneider.cedric@gmx.de ---
"Cannot put a <failed-type> into a <range-type> because it is not a <range-element-type>" would not need to be special-cased since the special-casing already happens in the template that retrieves the ranges element type (if I am understanding correctly, maybe the code creating the error message does not have all necessary information, though).

That aside, it certainly would have stopped me from opening this issue and instead go and read some more about ranges and strings in D because that error message clearly indicates that I am missing something. But I obviously do not speak for everyone and improving on the situation by adding a better error message is obviously not a fail-safe. I am pretty sure tiny things like that do help, though, considering that changing the string-handling everywhere is obviously not a possibility :-).

--
December 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14998

--- Comment #7 from Steven Schveighoffer <schveiguy@yahoo.com> ---
Except that's not exactly the reason. The reason is because no implementation exists to support it. Quite literally, the compiler tried to find a suitable mechanism, and couldn't.

Changing the error message to be more specific to this situation may read confusingly for another case where that *isn't* the problem.

I think it's possible we could make a special message for these situations, the code is already pretty dead set against allowing this (and has special cases to handle it). I just don't know what the correct answer is.

I was just reading this on the docs: http://dlang.org/phobos/std_range_primitives.html#.put

"r.putChar(e);     R accepts some form of string or character. put will transcode the character e accordingly."

In the implementation of putChar, it specifically forbids r to be a dynamic array. So we may want to add a note that R cannot be a dynamic array of char in this position (I'm unsure why, since it is possible). I will note that before the "putChar" function was added, it still didn't compile.

--
January 17, 2018
https://issues.dlang.org/show_bug.cgi?id=14998

Jack Stouffer <jack@jackstouffer.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=18256

--
January 17, 2018
https://issues.dlang.org/show_bug.cgi?id=14998

--- Comment #8 from Jack Stouffer <jack@jackstouffer.com> ---
(In reply to Steven Schveighoffer from comment #3)
> import std.utf;
> auto r = c.byCodeUnit;
> r.put('c');

This actually doesn't work. Making separate issue for it.

--
January 17, 2018
https://issues.dlang.org/show_bug.cgi?id=14998

--- Comment #9 from Jack Stouffer <jack@jackstouffer.com> ---
(In reply to Jack Stouffer from comment #8)
> (In reply to Steven Schveighoffer from comment #3)
> > import std.utf;
> > auto r = c.byCodeUnit;
> > r.put('c');
> 
> This actually doesn't work. Making separate issue for it.

Wait, my mistake, the underlying data needs to be char[] not string.

--
« First   ‹ Prev
1 2