【发布时间】:2020-12-20 20:05:35
【问题描述】:
我想实现一个通用 F# 类,它的类型参数肯定提供了一个名为“TryParse”的静态方法。除此之外,我希望我的班级在不再需要后得到正确处理。我想出了以下实现:
type Listener<'a when ^a : (static member TryParse : string -> ^a option)>() =
// construct the object here
let input : string = "" // get input
let res = (^a : (static member TryParse : string -> ^a option) input)
member this.Start() =
// ...
()
interface IDisposable with
member this.Dispose() =
// do cleanup
()
问题是:在两个成员(“开始”和“处置”)上,我收到以下错误:
Error: This code is not sufficiently generic. The type variable ^a when ^a : (static member TryParse : string -> ^a option) could not be generalized because it would escape its scope.
我可以通过用“内联”装饰它来修复 Start() 成员,但我无法对接口定义做同样的事情。
是否可以同时强制我的泛型类型来实现静态方法并定义类 Disposable ?
【问题讨论】:
-
类不能有SRTP,只有函数可以。
-
有趣的事实:没有 IDisposable 接口并且在“Start”成员中添加了“inline”说明符,Visual Studio 中的类类型检查,但随后出现链接错误。
标签: .net compiler-errors f# type-constraints