Caelan
Caelan

Reputation: 1040

F# Generics Constraints

I have this method on class:

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())

but when I build the project I get this error

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.

But all constraints are there. Am I missing something?

Edit:

If you need more code:

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

this is the open source project

Upvotes: 3

Views: 211

Answers (1)

Caelan
Caelan

Reputation: 1040

I don't know why that error message was displayed for my code but I found workaround and I committed on GitHub project. You can check the diff on this file because it's easier to read than describe it.

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

The link contains the diff that solves the question but I'll explain that in the next lines.

I solved it with a workaround, I don't know why it was displaying.

On the repository class (the class of repository in the question, not the class of the method) has these members:

let mutable dbSetFunc : Func<DbContext, DbSet<'TEntity>> = null
member this.DbSetFunc 
    with set (value) = dbSetFunc <- value
member this.DbSetFuncGetter() = dbSetFunc

but they generated the error, I found it trying commenting them and I changed them to these

[<DefaultValue>] val mutable dbSetFunc : Func<DbContext, DbSet<'TEntity>>
member this.DbSetFunc 
    with set (value) = this.dbSetFunc <- value
member internal this.DbSetFuncGetter() = this.dbSetFunc

And I can't explain why this solves the error, at least I hope this helps.

The method in the question remains the same.

Upvotes: 2

Related Questions