Warning FS3536: "(interface) is normally used as a type constraint in generic code" for extension method on interface in .NET 7 SDK
See original GitHub issueWhen compiling an extension method for an interface using .NET SDK 7 you now get a confusing FS3536 warning about type constraints. With .NET SDK 6 there was no such warning. I don’t see anything wrong with the code, so as far as I can tell the warning is a bug.
Repro steps
Try to compile the following console EXE:
type INormalInterface =
abstract member IntMember: int
module INormalInterfaceExtensions =
type INormalInterface with
static member extMethod (a: INormalInterface) =
()
printfn "Hello from F#"
Project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
Expected behavior
Compiles with no warnings, same as with .NET 6 SDK.
Actual behavior
$ dotnet build
MSBuild version 17.4.0+18d5aef85 for .NET
Determining projects to restore...
Restored /home/em/src/FS3536/FS3536.fsproj (in 468 ms).
/home/em/src/FS3536/Program.fs(6,37): warning FS3536: 'INormalInterface' is normally used as a type constraint in generic code, e.g. "'T when ISomeInterface<'T>" or "let f (x: #ISomeInterface<_>)". See https://aka.ms/fsharp-iwsams for guidance. You can disable this warning by using '#nowarn "3536"' or '--nowarn:3536'. [/home/em/src/FS3536/FS3536.fsproj]
Known workarounds
Use .NET 6 SDK by creating a global.json file or suppress that specific warning like the message says.
- Operating system: Ubuntu 18.04
- .NET Runtime kind (.NET Core, .NET Framework, Mono): .NET 6.0.12
- .NET SDK: 7.0.101
Issue Analytics
- State:
- Created 9 months ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Constraints on type parameters - C# Programming Guide
The type argument must be a reference type, either nullable or non-nullable. This constraint applies also to any class, interface, delegate, or ...
Read more >c# extension method for a generic class with interface as ...
1 Answer. Yes, this can be done by making the method generic and adding a generic type constraint to the method, as follows:...
Read more >Why does generic interface constraint mess up the use of ...
In your implementation of ITwo, you try to call the extension method on the object this ... When to use a non-generic interface...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No, sorry, not what I meant. 6.0.200 was released around February last year, so I would expect 7.0.200 to be released around same time this year. I just don’t know exact schedule.
OK, so basically it can take another year or so? So we’d better work around this by suppressing the warning and not wait for the fix?