Sergio0694 / PolySharp

PolySharp provides generated, source-only polyfills for C# language features, to easily use all runtime-agnostic features downlevel. Add a reference, set your C# version to latest and have fun! 🚀

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for Inline arrays in C# 12 preview?

sdcb opened this issue · comments

Is anyone working on this? I may be willing to do so, but I'm hesitant because I don't have much experience with source generators.

This is planned (along with all other new polyfills for C# 12), just waiting for the language to be stable first 🙂
The design of several of the new attribute types has changed over time, so I want them to stabilize before adding polyfills.

Hey Sergio, I love your work!

Following on from the C#12 theme, with .NET 8 now released I can’t wait to start using it!

Are you able to share the plans for C#12 support? Is there anything the community can do to help?

Thanks :)

Hmm, today I was curious how we can do things by ourselves. So I decided to play around with things myself in order to see how far I can get, and what happened was actually quite interesting.

After setting my csproj TFM to net481 I got several errors, but most obviously that the InlineArray attribute was missing.
So I just decided to copy the official InlineArrayAttribute.cs .NET source file into my project.
Which solved that part of the problems.

Another error shown about missing System.Runtime.InteropServices.MemoryMarshal.CreateSpan was shown by IntelliSense (but somehow not by the compiler output itself) at that point on my code where I assigned values to my internal array for all the indices.
I was eventually able to remove it by making my own public static partial class MemoryMarshal implementation with a CreateSpan method and modify that a bit so that it could compile on .NET Framework as well.

However, after these actions, the following more serious compiler error still remained:

error CS9171: Target runtime doesn't support inline array types.

To me, that suggests that the current situation is that inline arrays are actually impossible on .NET Framework at all, or at least that the Roslyn compiler itself does not allow you to do this at least.

So in that case PolySharp alone will not be able to solve it either I guess...

Oh, I forgot this one. Actually completed by #81.

"inline arrays are actually impossible on .NET Framework at all"

That is correct, it requires runtime support. This is why it's part of the "runtime supported" attributes which are dummy and don't actually do anything (like eg. [UnmanagedCallersOnly]), and are opt-in. They're only meant to make multi-targeted code cleaner.