-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
u8 literals should support literal concatenation #60896
Comments
Request seems sensible and useful, but I don't remember that we discussed it in LDM. Assigned to @AlekseyTs to drive discussion/resolution. |
@stephentoub, in order to expedite this and make chances of this happening feasible, I suggest you to put a concrete language proposal together. I.e. what specific language rules you propose to add/change around |
At first glance, inability to concatenate u8 literals appeared as a bug in implementation rather than missing language design; given how it is already possible to write it without the public ReadOnlySpan<byte> M() => "abc" + "def";
// without u8 suffix compiler still implicitly converts to UTF-8 bytes,
// instead of of Unicode / UTF-16 bytes (which I personally found unintuitive). |
That was not a bug, UTF-8 conversion of string constants and |
I'm proposing the language behave as if there's a: u8literal operator +(u8literal x, u8literal y); operator that produces a new literal that in turn yields the concatenation of all the bytes from ReadOnlySpan<byte> s = "abc"u8 + "def"u8; and that would be compiled identically to: ReadOnlySpan<byte> s = "abcdef"u8; Similarly, ReadOnlySpan<byte> s = "abc"u8 + "def"u8 + "ghi"u8; would be compiled identically to: ReadOnlySpan<byte> s = "abcdefghi"u8; as would: ReadOnlySpan<byte> s = ("abc"u8 + "def"u8) + "ghi"u8; and ReadOnlySpan<byte> s = "abc"u8 + ("def"u8 + "ghi"u8); This would only apply to literals and not to the natural type ReadOnlySpan<byte> s = "abc"u8 + (ReadOnlySpan<byte>)"def"u8; // error would be an error, as would: ReadOnlySpan<byte> s1 = ...;
ReadOnlySpan<byte> s2 = "abc"u8 + s1; // error |
Just to confirm, this proposal requires each literal to be successfully convertible to UTF-8 byte representation individually. Correct?
Even though this code will:
|
Yes. Otherwise a standalone @GrabYourPitchforks, thoughts? |
I'm totally fine with the compiler not allowing splits like this. If you're writing a u8 string literal, it's really weird (IMO) for people to use |
Implemented in #62044. |
Version Used:
32a81dd
Steps to Reproduce:
Expected Behavior:
Produces a result equivalent to:
This is valuable for very long UTF-8 literals such that you want to split them across lines.
Actual Behavior:
Fails to compile.
Relates to test plan #58848
The text was updated successfully, but these errors were encountered: