title: How do I implement this generic C# interface using F#?
body: <p>I want to create an object with <a href="https://github.com/jwt-dotnet/jwt/blob/d2a8fa2b7264dd046dc76f8e107baa6707e4c442/src/JWT/IJsonSerializer.cs" rel="nofollow noreferrer">this interface</a> in F#:</p>
  <pre class="lang-cs prettyprint-override"><code>namespace JWT
  {
      /// &lt;summary&gt;
      /// Provides JSON Serialize and Deserialize.  Allows custom serializers used.
      /// &lt;/summary&gt;
      public interface IJsonSerializer
      {
          /// &lt;summary&gt;
          /// Serialize an object to JSON string
          /// &lt;/summary&gt;
          /// &lt;param name=&quot;obj&quot;&gt;object&lt;/param&gt;
          /// &lt;returns&gt;JSON string&lt;/returns&gt;
          string Serialize(object obj);
  
          /// &lt;summary&gt;
          /// Deserialize a JSON string to typed object.
          /// &lt;/summary&gt;
          /// &lt;typeparam name=&quot;T&quot;&gt;type of object&lt;/typeparam&gt;
          /// &lt;param name=&quot;json&quot;&gt;JSON string&lt;/param&gt;
          /// &lt;returns&gt;Strongly-typed object&lt;/returns&gt;
          T Deserialize&lt;T&gt;(string json);
      }
  }
  </code></pre>
  <p>I have tried using an object expression but the generic <code>Deserialize</code> method is tripping me up:</p>
  <pre class="lang-ml prettyprint-override"><code>  let serializer =
      {
        new JWT.IJsonSerializer
          with
            member this.Serialize (o : obj) =
              failwith &quot;Not implemented&quot;
  
            member this.Deserialize (json : string) =
              let decoded : Result&lt;'t, string&gt; = 
                Decode.fromString payloadDecoder json
  
              match decoded with
              | Result.Ok (o : 't) -&gt;
                o
              | Result.Error error -&gt;
                failwith error
      }
  </code></pre>
  <p>The error is:</p>
  <blockquote>
  <p>This code is not sufficiently generic. The type variable 'a could not be generalized because it would escape its scope</p>
  </blockquote>
  <p><strong>How can I implement this?</strong></p>
  <hr />
  <pre class="lang-sh prettyprint-override"><code>$ dotnet --version
  3.1.300
  </code></pre>