With the recent addition of Spans, we can reduce a lot of allocations when dealing with strings. For example, substrings can be replaced with slices. However, there is one place where using strings is better:
string str = ...; switch (str) { case "string 1": break; case "string 2": break; case "string 3": break; case "string 4": break; }
With spans, you are forced to write boilerplate code:
ReadOnlySpan<char> span = ...; switch (span) { case var _ where span == "string 1": break; case var _ where span == "string 2": break; case var _ where span == "string 3": break; case var _ where span == "string 4": break; }
Which is identical to the if-else mess that switches aim to solve.
ReadOnlySpan<char> span = ...; if (span == "string 1") else if (span == "string 2") else if (span == "string 3") else if (span == "string 4")
It would be better if spans can be switched with constant strings.
ReadOnlySpan<char> span = ...; switch (span) { case "string 1": break; case "string 2": break; case "string 3": break; case "string 4": break; }
Special-casing spans as a compiler-known type already have precedent in stackalloc expressions, it can also be done here.
Possible extension to regular Span<char> can also be considered, but it is not as important as ReadOnlySpan<char>. We could also enable this with ReadOnlyMemory<char> and Memory<char>.
More possible extensions: switching (ReadOnly)Span<char> with char, (ReadOnly)Span<T> with T where T is a constant type.
Design meetingsorthoxerox, Unknown6656, YairHalberstadt, iamcarbon, jnm2 and 87 moreUnknown6656, TheUnlocked, stuartstein777, sa-exe, d-kr and 17 morealefcarlos, Mrxx99, wieslawsoltes, Nucs, Nyrest and 4 more
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4