Ronald
Ronald

Reputation: 667

F# compilation error related to inheritance

I have 2 interfaces defined in a C# component, where 1 interface inherits from another generic interface

public interface IRepository<T>
{
void Add(T obj);
void Update(T obj);
void Delete(T obj);
}
public interface IDataModelRepository :
    IRepository<DataModel>
{
}

I have an F# component where I define a generic type with a constructor parameter with the generic interface:

type Domain<T> ( getRepository:System.Func<IRepository<T>>) = ...

Then I define a derived type using the derived interface:

type DataModelDomain ( getRepository:System.Func<IDataModelRepository>) =
inherit Domain<DataModel> (getRepository)

This does not compile giving the error message on the DataModelDomain type:

The type 'IRepository' does not match the type 'IDataModelRepository'

However if I define those 'Domain' types in classes in a C# component in a similar way, it does compile. Is there a way to define the DataModelDomain type deriving from the generic Domain type and using the derived interface IDataModelRepository

Upvotes: 2

Views: 109

Answers (1)

TheQuickBrownFox
TheQuickBrownFox

Reputation: 10624

I think I managed to reproduce the problem in compiling F# code. And then fixed it by creating a new function, calling the other one inside it, and safely upcasting the result: (fun () -> getRepository.Invoke() :> IRepository<DataModel>)

Here's the whole thing:

type DataModel = DataModel

type IRepository<'T> =
    abstract member Add: 'T -> unit
    abstract member Update : 'T -> unit
    abstract member Delete : 'T -> unit

type IDataModelRepository =
    inherit IRepository<DataModel>

type Domain<'T> ( getRepository:System.Func<IRepository<'T>>) =
    do ()

type DataModelDomain ( getRepository:System.Func<IDataModelRepository>) =
    inherit Domain<DataModel> (fun () -> getRepository.Invoke() :> IRepository<DataModel>)
    do ()

Upvotes: 1

Related Questions