首页 > 解决方案 > 对象类型中的磅符号,在 F# 中

问题描述

我遇到过这个:

[<Interface>]
type ILogger =
    abstract Debug: string -> unit
    abstract Error: string -> unit 

[<Interface>] type ILog = abstract Logger: ILogger

module Log =
    let debug (env: #ILog) fmt = Printf.kprintf env.Logger.Debug fmt
    let error (env: #ILog) fmt = Printf.kprintf env.Logger.Error fmt

从该页面:https ://bartoszsypytkowski.com/dealing-with-complex-dependency-injection-in-f/

我想知道类型前面的“#”符号在做什么:让调试(环境:#ILog)...

该页面的解释是这样的:

秘诀在于#ILog 签名,这意味着我们的环境可以是任何实现ILog 接口的泛型类型。

但我试图找到这方面的文档,但我的谷歌搜索没有成功。我在哪里可以阅读更多关于它的信息?

我的理解是,您可以传递实现特定接口的对象,使用 # 的函数将自动将该对象转换为接口并使用它。

但是,为什么他们在这里将其实现为 2 个接口而不是单个接口呢?

标签: interfacef#

解决方案


推荐阅读