-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
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.