F# 泛型约束

3

我有一个类中的方法:

member this.GetDbSet<'TEntity, 'TDTO, 'TKey when 'TKey :> IEquatable<'TKey> and 'TEntity :> IEntity<'TKey> and 'TEntity : not struct and 'TDTO :> IDTO<'TKey> and 'TEntity : equality and 'TEntity : null and 'TDTO : equality and 'TDTO : null and 'TKey : equality>(repository : BaseRepository<'TEntity, 'TDTO, 'TKey>) = 
    repository.DbSetFuncGetter().Invoke(uow.Context())

但是在构建项目时,我遇到了这个错误。
This code is not sufficiently generic. The type variable 'TEntity when 'TEntity :> IEntity<'TKey> and 'TEntity : not struct and 'TEntity : equality and 'TEntity : null and 'TKey :> IEquatable<'TKey> and 'TKey : equality could not be generalized because it would escape its scope.

但是所有的限制条件都在那里。我有什么遗漏的吗?

编辑:

如果您需要更多的代码:

https://github.com/Ar3sDevelopment/Caelan.Frameworks.BIZ/blob/fsharp/Caelan.Frameworks.BIZ/Classes.fs

这是一个开源项目。

1个回答

2
我不知道为什么我的代码会显示那个错误信息,但是我找到了一个解决方法并提交到了GitHub项目中。你可以查看这个文件的差异,因为它比描述更容易阅读。

https://github.com/Ar3sDevelopment/Caelan.Frameworks.BIZ/commit/22898671635b4667c8741853af9cc86910e1ff5a#diff-d5779b1053a390520d2a4a0c643f3d68

该链接包含的 diff 文件解决了问题,但我会在下一行解释。
我用了一个变通方法解决了它,不知道为什么它会显示出来。
在存储库类(问题中存储库类的类,而不是方法的类)中有这些成员。
let mutable dbSetFunc : Func<DbContext, DbSet<'TEntity>> = null
member this.DbSetFunc 
    with set (value) = dbSetFunc <- value
member this.DbSetFuncGetter() = dbSetFunc

但是它们产生了错误,我试着将它们注释掉并改成了这些。
[<DefaultValue>] val mutable dbSetFunc : Func<DbContext, DbSet<'TEntity>>
member this.DbSetFunc 
    with set (value) = this.dbSetFunc <- value
member internal this.DbSetFuncGetter() = this.dbSetFunc

“我无法解释为什么这样可以解决错误,但至少我希望这有所帮助。”
“问题中的方法保持不变。”

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接