Question

I got the following types in my F# library (same file & namespace):

type HPart =
| Titles  of string list
| HLine   of string list

type HFile = HPart list

This library was added as reference in a C# project and I would like to create a function that takes an "HFile" as parameter.

The problem is the following synopsis doesn't work:

public void LoadFromTemplate(HFile template)

But this works:

public void LoadFromTemplate(FSharpList<HPart> template)

I don't understand why the first example doesn't work. Intellisense doesn't even show "HFile" in the completion list when I start to write it.

I thought HFile would be the same as FSharpList< HPart>. Does that means HFile is not a type ? And even if it's just a syntaxic sugar why can't I use it in my synopsis, makes the code clearer and that's one of the reason I choosed F# in the first place.

Was it helpful?

Solution

The problem is that HFile is an F# type abbreviation. In F#, this means that the type HFile is exactly the same thing as HPart list. This means that wherever you use one on the F# side, you can also use the other.

Sadly, type abbreviations do not have any direct representation in the .NET IL, and so F# compiles them just as meta-data understood by the F# compiler. This means that the type HFile does not really exist as a type from the .NET and C# view.

You could define it as a real type in F# - such as a simple discriminated union with a single constructor:

type HFile = HFile of HPart list

Or you could define a C#-style type alias in C# with using. This requires full path and has to be in each of the files where you want to use HFile:

using HFile = Microsoft.FSharp.FSharpList<YourFSharpNamespace.HPart>;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top