[#]dotnet/#csharp 13.0 params question. Given:
static Span M1(Span indices) => indices;
static Span M2(params Span indices) => indices;
static Span M3(scoped Span indices) => indices;
It seems params implies "scoped", and that thus M2 and M3 fail to compile with.
error CS8352: Cannot use variable 'scoped Span indices' in this context because it may expose referenced variables outside of their declaration scope
What options are there to "unescape" this?
=> More informations about this toot | More toots from nietras@mastodon.social
sharplab.io link:
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANmgExAGoAfAAQCYBGAWACgGA3AQygAIn2BedgWUoAKAAxp21MZQCUAbgYNy1JOwDKABxYA7ADzZNGAHz9qg9Vt36jegtjAwAzlJ5XNNu/bn1Fyszr2H+IQ0oFgBbe1UNP0t2a1sHJ24XNwdPb0jzfyM+AGZBe0g1GAIM6IC490Tk+I8GIA==
=> More informations about this toot | More toots from nietras@mastodon.social
if changing code to (to compile):
var m1 = M1([0, 1, 2]);
var m2 = M2(0, 1, 2);
var m3 = M3([0, 1, 2]);
foreach (var m in m1) { Console.WriteLine(m); }
static Span M1(Span indices) => indices;
static Span M2(params Span indices) => indices.ToArray();
static Span M3(scoped Span indices) => indices.ToArray();
One can observe the same code is generated at the call site. And hence M1 works fine.
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0ATEBqAPgAQCYBGAWACgKA3AQygAIBbY+gXnoFliAKAbQAY09YkMIBdAJQBuanSaE2nQt0HDR02Q0YBmRR219VI+uI2VyAM2gwaYABb1utLfQCWAOybEJ9AN718YgBObkZpegBfCgpApHoAZQAHGncAHg8AFwA+Th4klPT3bLd3DFcwGABnH1YcjzKKyplyWITktMycjmVkqBpGSraCzpKGqpq60vKqgDoAFQgAQSg+gE9uM1b8jqKug0rIRJgMIZ3i+unqtkmxyvmllZp1syA=
=> More informations about this toot | More toots from nietras@mastodon.social
The #csharp language design seems pretty clear on this is intended....
"A params ReadOnlySpan is implicitly scoped. The parameter cannot be annotated with [UnscopedRef] and cannot be declared scoped explicitly. Within the params method, the compiler will use escape analysis to report diagnostics if the span is captured or returned."
https://github.com/dotnet/csharplang/blob/main/proposals/params-span.md
I am perhaps wondering why no option to unescape this?
cc @jaredpar
=> More informations about this toot | More toots from nietras@mastodon.social
I just want to return the span to the caller again inside a ref struct which is definitely safe to do, but this prevents that, why? And can this be safely/performantly circumvented?
=> More informations about this toot | More toots from nietras@mastodon.social
@nietras Weird, unlike what is claimed in this spec, I can annote it with UnscopedRef and then it works π€
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0ATEBqAPgAQCYBGAWACh8AGAAn2IDoARASwEMBzAOwgGcAXFmF4MAwhAwwAglzYAbAJ68WvANwUKANzZQammgF4aAWUIAKKmhrErhAJTryFekhoBlAA5suAHhZd+AD4TYjNPbz8A4P8MIRheO0NorliweMcXdy9ffyCTcwBtAFUuXkgPGAwAJRgAMwBdGi8oNgBbXiyI3OTU+MSDHri1CgB6EczwnKiTAGYzMogKjE6pvJih/sG04fIgA==
=> More informations about this toot | More toots from xoofx@mastodon.social
@xoofx even weirder I thought I tried that in VS but it actually does work. So perhaps spec is not up to date, well I will try using that then :)
=> More informations about this toot | More toots from nietras@mastodon.social
@nietras
At first I thought that said "I just want to return the SPAM to the caller again" - don't we all? π
=> More informations about this toot | More toots from SmartmanApps@dotnet.social
@SmartmanApps ha yeah that would be nice too!
=> More informations about this toot | More toots from nietras@mastodon.social This content has been proxied by September (3851b).Proxy Information
text/gemini