gpt4 book ai didi

.net - 非统一实例化的用法是什么意思?

转载 作者:行者123 更新时间:2023-12-01 02:20:55 25 4
gpt4 key购买 nike

我无法编译以下代码:

open Genotype
open Genome

type IAgent =
abstract member CrossoverA: Genome<'T> -> unit

type internal AgentMessage<'T> =
| GetEnergy of AsyncReplyChannel<int>
| CrossoverMessage of Genome<'T>
| CompareMessage of Genome<'T>

type Agent<'T>(initialLifeEnergy : int, genotype : IGenotype<'T>) =
let LifeEnergy = initialLifeEnergy
let mailbox = new MailboxProcessor<AgentMessage<'T>>(fun inbox ->
let rec loop =
async {
let! (msg) = inbox.Receive()
printfn "Message received: %O" msg
match msg with
| GetEnergy reply ->
reply.Reply(LifeEnergy)
| CrossoverMessage genome->
printfn "crossover"
| CompareMessage fenome ->
printfn "compare"
}
loop )
do
mailbox.Start()


member this.CrossoverA(genomeIn: Genome<'T>) = (this :> IAgent).CrossoverA(genomeIn: Genome<'T>)
interface IAgent with
member this.CrossoverA(genomeIn: Genome<'T>) =
printfn "Crossover"
mailbox.Post(CrossoverMessage genomeIn)
member this.CrossoverA(genomeIn: Genome<'T>) 行有错误:

Error 1 The generic member 'CrossoverA' has been used at a non-uniform instantiation prior to this program point. Consider reordering the members so this member occurs first. Alternatively, specify the full type of the member explicitly, including argument types, return type and any additional generic parameters and constraints.

Error 2 One or more of the explicit class or function type variables for this binding could not be generalized, because they were constrained to other types


也符合 mailbox.Post(CrossoverMessage genomeIn) :

Error 3 The type ''T' does not match the type ''a'


我没有在项目的任何地方使用变量“a”。此外,名称 CrossoverA 仅在此文件中使用。我感到困惑,项目中的其他类是用类似的打字模式创建的,而且效果很好。

最佳答案

它可能无法推断 CrossoverA 的返回类型由于对您的接口(interface)实现的前向调用。类型推断只能使用当前点之前可用的类型信息。理想情况下,接口(interface)会将调用转发给类,而不是相反。这将解决推理问题。

编辑 - 另一个问题似乎是使用类型 arg 'TIAgent.CrossoverA ,它没有在类型上定义。

关于.net - 非统一实例化的用法是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com