5

I am in a situation where I believe type providers would be useful. Consuming type provider in F# could be the way I finally dip my toes in F#.

However, I understand that writing type providers is hard. It may not be a suitable first task in F#.

Is there a reason why it would be problematic to write the type provider in C# while consuming it in F#? From what I understand, it's just a matter of implementing a .NET interface and maybe decorate with some attributes to make the F# compiler pick up on it.

  • 1
    I haven't tried it, but I suggest you take one written in F#, compile it, then disassemble it in C#, to see what it looks like. – Frank Hileman Aug 23 '18 at 21:26
  • You can write any .NET code using any .NET language (VB.NET, C++/CLR, C#, F#). However, you cannot get the F# compiler to compile C# code nor vice-versa, you need to put them into separate assemblies. You do not need an interface nor do you need any attributes. .NET code all compiles down into exactly the same thing (IL / Interpreted Language), which can be picked up from any other .NET project in any other .NET language – Ben Cottrell Aug 23 '18 at 21:27
  • @FrankHileman That is a good idea. There are some very basic type providers in the Fsharp.data library. However, I fear that simple F# quickly can generate very messy C# code. So, the resulting C# may not be how you would be writing the component in C#, but it's worth a shot. – Tormod Aug 23 '18 at 21:39
  • 1
    @Tormod, write tests, put very messy C# code as implementation to make tests pass. Then refactor it to pretty clean simple C# ;) – Fabio Aug 24 '18 at 11:12
  • 1
    @BenCottrell Not all F# code is directly convertible to VB.NET and C#. The IL of 'tail in most implementation of tail recursive in F# is not supported in VB.NET and C#. Also F# computation expression is not easily convertible to C#/VB.NET. – eriawan Aug 3 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.