question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 issue

When 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:closed
  • Created 9 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
vzarytovskiicommented, Jan 11, 2023

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?

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.

0reactions
loop-evgenycommented, Jan 11, 2023

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found