Pattern matching with UTF-8 String Literals #6036
-
C# 11 preview blog post includes UTF-8 String Literals, and Pattern matching with spans (specifically: It would be interesting if both can be combined, allowing pattern matching against For example: static HttpMethod ParseHttpMethod(ReadOnlySpan<byte> method)
{
return method switch {
"GET"u8 => HttpMethod.Get,
"POST"u8 => HttpMethod.Post,
...
_ => HttpVerb.Unknown };
} This is interesting for use-cases that involve parsing UTF-8. (Edited on 5/11/2022 by @stephentoub to include u8 suffix) |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
I've created an issue for this: #6037. |
Beta Was this translation helpful? Give feedback.
-
It looks like this feature is mentioned in the Test plan for Utf8StringLiterals feature.
So, maybe this is already part of the plan for C# 11? cc @AlekseyTs |
Beta Was this translation helpful? Give feedback.
-
I would definitely want this as i currently deal with json encoded in utf8 and being unable to use pattern matching would be both PITA and confusing when same thing works for utf-16 strings but for utf8 doesnt despite utf8 strings looking and even behaving like constant in pattern branches as long as it isnt interpolated (with exception being interpolated out of constants but support for this isnt necessary) |
Beta Was this translation helpful? Give feedback.
-
@AlekseyTs @KathleenDollard is this part of the plan for C# 11 already? If so, we can close the discussion. |
Beta Was this translation helpful? Give feedback.
-
Discussed in LDM on May 11th, 2022. https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-05-11.md#pattern-matching-with-utf-8-string-literals Conclusion: We will need a specification. I've opened #6161 for this, and put it in the any time milestone, which means a specification is open for community contribution. |
Beta Was this translation helpful? Give feedback.
-
The priority of this feature definitely needs to be raised. |
Beta Was this translation helpful? Give feedback.
Discussed in LDM on May 11th, 2022. https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-05-11.md#pattern-matching-with-utf-8-string-literals
Conclusion: We will need a specification. I've opened #6161 for this, and put it in the any time milestone, which means a specification is open for community contribution.