静态解析类型参数 [英] 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) :?> A< 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的Type Classes实现产生的解决方案. 为了使F#环境与.Net环境交互",已经实现了接口,抽象类等,出于样式统一的原因,在上下文中使用了实现接口,抽象类等的代码不需要与.Net库进行交互,在我看来,这是开销"(尽管F#是一种多范式语言).这就是为什么我发现Haskell的Type Classes实现非常优雅的原因.下面,我通过F#实现了"Haskell类型类"代码来解决我的问题.
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屋!