Issue 20030 - Variant concat of char and string doesn't work
Summary: Variant concat of char and string doesn't work
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-07-05 11:25 UTC by Simen Kjaeraas
Modified: 2024-12-01 16:35 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2019-07-05 11:25:16 UTC
The following code throws a VariantException complaining about different types (char and immutable(char)[]):

unittest {
    import std.variant;
    Variant v = "";
    v ~= 'a';
}

It's correct in some ways, but clearly a more useful behavior would be to concat the string and char and move on.
Comment 1 shove 2019-07-08 02:10:01 UTC
(In reply to Simen Kjaeraas from comment #0)
> The following code throws a VariantException complaining about different
> types (char and immutable(char)[]):
> 
> unittest {
>     import std.variant;
>     Variant v = "";
>     v ~= 'a';
> }
> 
> It's correct in some ways, but clearly a more useful behavior would be to
> concat the string and char and move on.

This shouldn't be a bug. Variant's design has strict restrictions on type qualifiers, because string = immutable (char) [], so the ~ operator restricts char. v ~= cast (immutable char)'a'; is possible because its qualifier is consistent.

For these limitations, you can see the unittest:
https://github.com/dlang/phobos/blob/master/std/variant.d#L2655

There are many anomaly collection behaviors for qualifier inconsistencies.

I don't know whether it was intentionally designed to do this, or because it was temporarily done for the sake of simplicity. I think, if there are no other factors, it would be better to fix the variant implementation and remove these restrictions.
Comment 2 dlangBugzillaToGithub 2024-12-01 16:35:11 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9774

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB