静态解析的类型参数 [英] Statically Resolved Type Parameters
问题描述
以下(简化的)片段取自我正在实现的应用程序,该应用程序始终使用静态解析的类型参数.
The following (simplified) snippet is taken from an application I'm implementing which consistently uses Type Parameters resolved statically.
type A< ^B when ^B : (static member MyMember : Unit -> Unit)> = {
Field : unit
}
type TestA = {
AField : A< BTy >
}
and BTy = {
BField : Unit
} with
static member MyMember () = ()
当我定义字段 AField (AField : A< BTy >
) 的类型时,IntelliSense 给了我以下错误:类型BTy"不支持任何名为MyMember"的运营商.
IntelliSense gives me the following error when I define the type of field AField (AField : A< BTy >
) which is: The type 'BTy' does not support any operators named 'MyMember'.
已编辑.分别声明它们是可行的,但是如果我有相互引用并且我不能声明第三种类型放在顶部,其中包含两种类型的公共信息.我应该怎么做才能避免这个问题?无论如何,如果我在定义下面定义 let pluto = ("" :> obj) :?><BTy >
它可以工作,我想是因为这两种类型都可以从 let 绑定中看到.
EDITED.
Declaring them separately works, but if I have a mutual reference and I cannot declare a third type to put in the top which contains the common Infos of the two types. What should I do to avoid this problem? Anyway, if I define below the definitions let pluto = ("" :> obj) :?> A< BTy >
it works, I imagine because both types are visible from the let binding.
推荐答案
你的实现@Tomas 满足了这个问题,但你的解决方案的风格有些犹豫,因为它不尊重函数式编程范式.我想到了由 Haskell 的类型类实现产生的解决方案.接口、抽象类等已被实现以允许 F# 环境与 .Net 环境交互",出于风格统一的原因,在上下文中使用实现接口、抽象类等的代码.不需要与 .Net 库进行交互,在我看来这是一种开销"(尽管 F# 是一种多范式语言).这就是为什么我发现 Haskell 的类型类的实现非常优雅的原因.下面我通过 F# 实现了Haskell Type Class"代码来解决我的问题.
Your implementation @Tomas satisfies the question but there is some hesitation over the style of your solution because it does not respected the functional programming paradigm. I thought to a solution arising from the Type Classes implementation of Haskell. Interfaces, abstract classes, etc. have been implemented in order to allow the F# environment to "interact" with the .Net environment, for a reason of uniformity of style the use of code which implements interfaces, abstract classes etc. in a context where no need of interact with the .Net libraries, it is a "Overhead" in my opinion (although F# is a multi-paradigm language). This is the reason why I found the implementation of the Type Classes of Haskell very elegant. Below I implemented through F# the "Haskell Type Class" code to solve my problem.
type Operations< ^a> =
{
MyMember : unit -> unit
}
type A< ^a> = {
Operations : Operations< ^a>
}
and TestA = {
AField : A< BTy >
}
and BTy = {
BField : Unit
}
let it =
let BTy_operations : Operations< BTy > = { MyMember = fun () -> () }
let A_of_BTy = { Operations = BTy_operations }
{ AField = A_of_BTy }
这篇关于静态解析的类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!