Ancestors

Written by nietras πŸ‘Ύ on 2024-11-24 at 12:09

[#]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

Toot

Written by nietras πŸ‘Ύ on 2024-11-24 at 12:09

sharplab.io link:

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANmgExAGoAfAAQCYBGAWACgGA3AQygAIn2BedgWUoAKAAxp21MZQCUAbgYNy1JOwDKABxYA7ADzZNGAHz9qg9Vt36jegtjAwAzlJ5XNNu/bn1Fyszr2H+IQ0oFgBbe1UNP0t2a1sHJ24XNwdPb0jzfyM+AGZBe0g1GAIM6IC490Tk+I8GIA==

=> More informations about this toot | More toots from nietras@mastodon.social

Descendants

Written by nietras πŸ‘Ύ on 2024-11-24 at 12:24

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

Written by nietras πŸ‘Ύ on 2024-11-24 at 12:31

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

Written by nietras πŸ‘Ύ on 2024-11-24 at 12:32

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

Written by Alexandre Mutel on 2024-11-24 at 13:55

@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

Written by nietras πŸ‘Ύ on 2024-11-24 at 14:00

@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

Written by πŸ’‘πš‚π—†π–Ίπ—‹π—π—†π–Ίπ—‡ π™°π—‰π—‰π—ŒπŸ“± on 2024-11-24 at 21:02

@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

Written by nietras πŸ‘Ύ on 2024-11-24 at 21:26

@SmartmanApps ha yeah that would be nice too!

=> More informations about this toot | More toots from nietras@mastodon.social

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113537853638720204
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
305.363329 milliseconds
Gemini-to-HTML Time
1.939364 milliseconds

This content has been proxied by September (3851b).