dotnet / dotNext

Next generation API for .NET
https://dotnet.github.io/dotNext/
MIT License
1.56k stars 119 forks source link

Compilation error when I make a struct implement IOptionMonad<T, TSelf> #179

Closed carpe7 closed 1 year ago

carpe7 commented 1 year ago

Hi, If I make a struct implement IOptionMonad<T, Self>, I get the following compilation errors in Visual Studio 2022 17.6.4, targeting .NET 6.0 (I have .NET SDK 6.0.411, .NET SDK 7.0.302, .NET SDK 7.0.304 and .NET SDK 7.0.305 installed):

error CS0570: 'IOptionMonad<T, TSelf>.explicit operator T(in TSelf)' is not supported by the language error CS0570: 'IOptionMonad<T, TSelf>.operator true(in TSelf)' is not supported by the language error CS0570: 'IOptionMonad<T, TSelf>.operator false(in TSelf)' is not supported by the language

I have opted in for preview features in all known ways: EnablePreviewFeatures, RequiresPreviewFeaturesAttribute, disabling CA2252. The errors disappear if I include the definition of IOptionMonad<T, TSelf> (verbatim) from dotNext source code into my project. I only need IOptionMonad<T, TSelf>, i.e., the interfaces it depends on (IOptionMonad, ISupplier...) do not need to be copied and are so taken directly from dotNext's NuGet package reference. If I add my struct to the source code of the dotNext solution and I compile it, no error generated either. If I comment the ": IOptionMonad<...>" interface implementation declaration, but keep the apparently unsupported operators in my struct (simply they are not part of the implementation of IOptionMonad), the errors disappear again. So the compiler is perfectly capable of dealing with such operators. The problem arises when they are the implementation of IOptionMonad<T, TSelf>. I have also tried Visual Studio 2022 17.7.0 Preview 2.0 targeting .NET 8.0 to no avail. The only change is that each error is triggered twice on the same file and line. Quite intriguing.

Can anybody help me?

Thank you very much.

sakno commented 1 year ago

Well... It is preview feature in .NET 6. Probably, it works differently in .NET 7. This is an issue of Roslyn compiler. You can try to use the following combination of settings inside of csproj file:

<TargetFrameworks>net6.0</TargetFrameworks>
<LangVersion>preview</LangVersion>

Also, try to add global.json file to your project folder. Here is example: https://github.com/dotnet/dotNext/blob/master/src/global.json. This file restricts dotnet tool with .NET 6 SDK when building a project.

carpe7 commented 1 year ago

It happens with the preview language version as well and all .NET versions all the way up to 8.0. That interface is completely unusable outside the project it was implemented at in the first place. I'm closing the issue and moving it into a Roslyn bug.

Thank you very much.

sakno commented 1 year ago

MS gives no promises about preview features. It's very unlikely that they fix that.

carpe7 commented 1 year ago

I can't find any statement in the documentation about static interface virtual methods still being a preview feature in .NET 8 and C# 12. As the issue still reproduces there, I expect the bug not to be ignored by the Roslyn team.