Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

Initialize instance of internal discriminated union from F# to C#

I have a discriminated union type exported via a dll from F# to be used in C#:

type Argument =
  | IntValue
  | FloatValue
  | BoolValue of bool

In C# I can initialize something like Argument.NewBoolValue(true), but the other two types IntValue and FloatValue are not type generated via the F# compiler and hence marked as internal, is there a way to initialize them in C#?

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the discriminated union case doesn't have any payload, there is no NewXXX method generated. Because all instances of such union case will be the same and there is no need in creation function. So use staic properties instead


Internally these properties just return singleton instance of corresponding usecase:

internal static readonly Argument _unique_IntValue = new _IntValue();

public static Argument IntValue => _unique_IntValue;

More interesting details can be observed in this decompiled snippet.

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers


48.4k users