Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bound interface in F#

I am trying to use C# library in F# so it would be very much specific case. I using Servicestack with F#. Now, I am trying to wire up class with interface using method

RegisterAutoWiredAs<T,'TAs>()

signature. Here is 'T is having constraint that it has to implement 'TAs. It works fine in C# code.

But F# is having constraint while using interface.

let f:IFoo = Foo() // will give type error
let fi:IFoo - Foo() :> IFoo // will work

Here Foo has implemented IFoo. So, this part is quite different than C# equivalent. Now, above signature is giving type error if I do like this

container.RegisterAutoWiredAs<Foo,IFoo>()

And there is noway to do casting while giving parameter.

Here is line from original project I am trying to run. Everything in this code works other than this part and also sadly other equivalent methods are also failing.

And here is the error I am getting in that project

This expression was expected to have type 'MemoryChatHistory' but here has type 'IChatHistory'

like image 452
kunjee Avatar asked May 24 '26 01:05

kunjee


1 Answers

F# does not support implicit interface implementations.

I think you may be able to work around this in this instance by making IChatHistory an abstract class rather than an interface (using [<AbstractClass>] attribute).

EDIT:

Nope, I had a chance to play around with it today. I think it's impossible to call this method directly with those type parameters from F#. See this question

How do I translate a `where T : U` generic type parameter constraint from C# to F#?

for a little more discussion.

You might be able to work around this by using reflection to call the method.

like image 150
Brian Avatar answered May 26 '26 15:05

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!