【发布时间】:2016-04-12 19:02:56
【问题描述】:
假设我有两种不同的库类型:
type Foo = { foo : string }
type Bar = { bar : int32 }
我想实现适用于Foo 或Bar 实例的通用函数zoo。 我无法更改 Foo 和 Bar,因为它们是库代码的一部分。
这是我第一次尝试使用类型扩展和内联函数,如 here 所述:
// Library.fs
module Library
type Foo = { foo : string }
type Bar = { bar : int32 }
// Program.fs
type Foo with
static member zoo (f : Foo) = "foo"
type Bar with
static member zoo (b : Bar) = "bar"
let inline zoo (x : ^t) =
(^t : (static member zoo : ^t -> string) x)
let f = zoo { foo = "1" } // error FS0001: The type 'Foo' does not support the operator 'zoo'
为什么内联函数定义不依赖类型扩展?如何在不更改初始 Foo 和 Bar 类型定义的情况下解决我的问题?
【问题讨论】: