【问题标题】:F# Generics ConstraintsF# 泛型约束
【发布时间】:2014-06-26 12:26:07
【问题描述】:

我在课堂上有这个方法:

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

这是开源项目

【问题讨论】:

    标签: .net generics f# constraints f#-3.0


    【解决方案1】:

    我不知道为什么我的代码会显示该错误消息,但我找到了解决方法并在 GitHub 项目上提交。您可以检查此文件的差异,因为它比描述更容易阅读。

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

    该链接包含解决问题的差异,但我将在接下来的几行中解释。

    我通过解决方法解决了它,我不知道它为什么会显示。

    在存储库类(问题中存储库的类,而不是方法的类)上有这些成员:

    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
    

    而且我无法解释为什么这可以解决错误,至少我希望这会有所帮助。

    问题中的方法保持不变。

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多