繁体   English   中英

在 F# 记录上实现具有通用参数的接口

[英]Implementing an interface with a generic parameter on a F# record

我正在尝试在F# 记录上实现Microsoft.Extensions.Logging.ILogger (为简洁起见,复制如下)

using System;

namespace Microsoft.Extensions.Logging
{
    public interface ILogger
    {
        void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter);
        bool IsEnabled(LogLevel logLevel);
        IDisposable BeginScope<TState>(TState state);
    }
}

这是记录实现。

  type ILoggerRcd<'TState> = 
    {
      BeginScope : 'TState -> IDisposable
      IsEnabled : LogLevel -> bool
      Log : LogLevel * EventId * 'TState * exn * Func<'TState,exn,string> -> unit
    }
    interface ILogger with
          override this.BeginScope(state : 'TState): IDisposable = 
              this.BeginScope (state)
          override this.IsEnabled(logLevel: LogLevel): bool = 
              this.IsEnabled logLevel
          override this.Log(logLevel: LogLevel, eventId: EventId, state : 'TState, ``exception``: exn, formatter: Func<'TState,exn,string>): unit = 
              this.Log(logLevel, eventId, state, ``exception``, formatter)

但是,我在 BeginScope 上收到此错误: One or more of the explicit class or function type variables for this binding could not be generalized, because they were constrained to other types

日志上The generic member 'Log' has been used at a non-uniform instantiation prior to this program point. Consider reordering the members so this member occurs first. Alternatively, specify the full type of the member explicitly, including argument types, return type and any additional generic parameters and constraints. The generic member 'Log' has been used at a non-uniform instantiation prior to this program point. Consider reordering the members so this member occurs first. Alternatively, specify the full type of the member explicitly, including argument types, return type and any additional generic parameters and constraints.

我已经阅读了一些关于 fsharp 编译器本身的问题,但似乎没有什么是我遇到的这种情况。 这是可以做到的吗?

ILogger接口要求您可以记录任何类型的对象,但您尝试仅记录类型为'TState

获取BeginScope的签名:

IDisposable BeginScope<TState>(TState state);

看到那里的<TState>位了吗? 这是一个通用参数。 这个签名意味着每次任何人调用这个方法时,他们都可以选择一个类型TState用于该调用。

同样:调用者选择泛型类型,而不是实现者。

例如:

let l : ILogger = ...
l.BeginScope 42   // TState = int
l.BeginScope true // TState = bool

这意味着您的BeginScope实现必须能够使用任何类型,而不仅仅是创建ILoggerRcd记录的类型。

这里的问题是您试图约束ILogger'TState以匹配ILoggerRcd'TState ,但这不是您的选择。

要看到这一点,请注意ILogger.BeginScope的调用者可以在一次调用中传递一个int state,然后在对同一个ILogger实例的另一次调用中传递一个string state。 您的实现试图阻止这种情况,因此会出现编译器错误。

我能看到的唯一方法是在你的类型中使用方法而不是函数记录。 我认为没有任何方法可以使用香草 F# 记录来做你想做的事。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM