有没有办法在 F# 的类型约束中定义新的类型参数?

Is there a way to define new type parameters in type constraints with F#?

我似乎找不到做这样的事情的方法:

type Instance<'Aggregate when 'Aggregate :> Aggregate.T<'State,'Event,'Failure>> = {
  ...
  Aggregate: 'Aggregate
  CurrentState: 'State
  ...
}

请注意,我想:

这当然不会编译,因为'State没有定义。在保留实例含义的同时,还有其他方法可以做到这一点吗?

我想到了几个简单的方法,但是都失去了意义。例如。 Instance<'State,'Event,'Failure>Instance<'Aggregate> 不太一样。

所有类型参数必须是显式的,因此您需要包括 'State'Event'Failure:

type Instance<'Aggregate,'State,'Event,'Failure when 'Aggregate :> Aggregate.T<'State,'Event,'Failure>> = {
  ...
  Aggregate: 'Aggregate
  CurrentState: 'State
 ...
}

但是,在使用这种类型时,您应该很少需要显式指定那些额外的参数。创建实例时,编译器应该能够推断出它们,并且您可以对某些参数使用匿名 _ 占位符,即使您必须显式命名 Instance<_,_,_,_> 类型。