prydain
prydain

Reputation: 365

Use non Discriminated Union types from F# in C#

I have declared some data types in a F# library that I want to fill from C# code. The problem I encountered is that only DU's get "exported" as a class, consider this example file Test.fs:

module Test

type SimpleType = string

type SimpleList = string list

type SimpleDU =
     | A
     | B

type SimpleRecord = { Text : string }

I was confused at first when just referencing the F# project wouldn't allow me use the SimpleType and SimpleList types in C#. I looked at the resulting F# library with ILDasm and found only code for the SimpleDU and SimpleRecord type which are perfectly accessible in C# code.

Is there a way to "export" non DU types so they are usable in C# or do I have to declare every non DU type as an explicit record?

Upvotes: 1

Views: 194

Answers (1)

Lee
Lee

Reputation: 144126

The definitions of

type SimpleType = string
type SimpleList = string list

are type abbreiviations which are eliminated during type checking and do not create new types. These are described in the specifition:

8.3 Type Abbreviations

Type abbreviations define new names for other types. For example:

type PairOfInt = int * int

Type abbreviations are expanded and erased during compilation and do not appear in the elaborated form of F# declarations, nor can they be referred to or accessed at runtime.

Upvotes: 5

Related Questions