3

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'

labilbe
  • 3,501
  • 2
  • 29
  • 34
kunjee
  • 2,739
  • 1
  • 23
  • 38
  • What error are you getting? – Fyodor Soikin Apr 16 '17 at 07:31
  • @FyodorSoikin type mismatch. I can't use Foo with IFoo in method signature. I have updated the question with error message. – kunjee Apr 16 '17 at 07:44
  • This call to `RegisterAutoWiredAs` cannot produce such error. Are you sure it's happening on that line and not on a different one? – Fyodor Soikin Apr 16 '17 at 09:25
  • @FyodorSoikin have you tried with my code? Sorry but couldn't get your question properly. I'm pretty much sure that error is because of F# compiler as things are working with C#. – kunjee Apr 16 '17 at 09:41
  • Are you sure that the error is on that exact line and not on another one? Can you maybe post the full error message (with the line number included)? – Fyodor Soikin Apr 16 '17 at 16:11
  • @FyodorSoikin I have shared the line number. Just to allow compiling I have commented it. Just un-comment it and you can see the error. Here is line - https://github.com/fun-servicestack/fun-chat-dotnetcore/blob/master/src/Chat/Startup.fs#L268 – kunjee Apr 16 '17 at 17:01
  • It is Easter Sunday. I do not have a computer near me, much less one with F# compiler installed. I cannot download your project and try to compile it myself. I can, however, tell you that the error you posted happens when passing around values, _not_ when specifying generic arguments. This makes me think that this error is likely happening on a different line, not the line you pointed out. This is why I'm asking you to post the whole error message that the compiler prints out, including the error code and line number. – Fyodor Soikin Apr 16 '17 at 21:07
  • @FyodorSoikin oh sorry. That is complete error compiler is giving me. I will look into more detail and will comment on it. – kunjee Apr 17 '17 at 03:37
  • No, it's not complete. When the compiler prints out an error message, it is prepended with an error code (e.g. "FS0123") and the line/column. Are you running the compiler from Visual Studio and looking in the "errors" window? If so, you're not seeing the whole output. Try opening the "Output" window and switching to "Build" output. – Fyodor Soikin Apr 17 '17 at 12:17
  • @FyodorSoikin I'd agree with your initial comment that this doesn't sound like an error on that line. I've since downloaded and attempted to build and you do get the error in the question (code FS0001), and it does refer to that line. – Charles Mager Apr 17 '17 at 16:33

1 Answers1

1

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.

Community
  • 1
  • 1
Brian
  • 117,631
  • 17
  • 236
  • 300