Getting started

Installing the package

To install the latest version of FSharpWrap using the .NET CLI, use the following:

1: 
dotnet add package FSharpWrap

Using generated code

In order to use the generated code in your project, add the following element to your project file in an <ItemGroup> above all other <Compile> items:

1: 
<Compile Include="$(FSharpWrapOutputFile)" />

Because the generated code is quite large by default, the following should also be added to a .gitignore file:

1: 
*.autogen.fs

Any instance methods, constructors, instance read-only fields, and get-only properties on classes and immutable structs will then have F# functions, active patterns, or computation expressions generated for them.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
open System.Collections.Generic
open System.Collections.Immutable

// Constructors
let stack: Stack<_> = [ 1..10 ] |> Stack.ofSeq

// Instance method call
let a: bool = Stack.contains 5 stack

// Get-only properties
let b: int = Stack.count stack
let c: string =
    let nums = stack.ToImmutableList()
    match nums with
    | ImmutableList.IsEmpty -> "read-only"
    | _ -> "not read-only"

// Computation expressions
let d: Stack<_> =
    Stack.expr {
        for num in stack do num
        11;
        12;
        13;
    }

d :> seq<_> |> printfn "d is %A"
d is seq [13; 12; 11; 1; ...]

Filtering generated code

FSharpWrap allows entire assemblies and namespaces to be included or excluded from code generation, which helps reduce compilation times by generating only code that you will use. Note that assemblies can only either be included or excluded, and that namespaces can only either be included or excluded.

To only include certain assemblies in code generation, use the following:

1: 
2: 
3: 
4: 
<ItemGroup>
  <!-- Only assemblies with the following names will be included in code generation -->
  <FSharpWrapIncludeNames Include="System.Collections.Immutable;Expecto" />
</ItemGroup>

To only exclude certain assemblies from code generation, use the following:

1: 
2: 
3: 
<ItemGroup>
  <FSharpWrapExcludeNames Include="System.Reflection.MetadataLoadContext;FSharp.Core" />
</ItemGroup>

Following the inclusion or exclusion of assemblies, FSharpWrap will also include or exclude any specified namespaces. Note that in order to include or exclude any "child" namespaces, their names must be specified as well.

To only include the types in certain namespaces, use the following:

1: 
2: 
3: 
<ItemGroup>
  <FSharpWrapIncludeNamespaces Include="System.Collections.Immutable;System.Collections" />
</ItemGroup>

To only exclude types in certain namespaces, use the following:

1: 
2: 
3: 
4: 
<ItemGroup>
  <!-- Only the types in the System.Reflection, Microsoft.FSharp.Core, or FParsec namespaces are excluded -->
  <FSharpWrapExcludeNamespaces Include="System.Reflection;Microsoft.FSharp.Core;FParsec" />
</ItemGroup>

Code Generation Limitations

Only public members are included in code generation.

Currently, any non-readonly structs (structs not marked IsReadOnly in F#) are excluded from code generation.

namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.Collections.Immutable
val stack : Stack<int>
Multiple items
type Stack<'T> =
  new : unit -> Stack<'T> + 2 overloads
  member Clear : unit -> unit
  member Contains : item:'T -> bool
  member CopyTo : array:'T[] * arrayIndex:int -> unit
  member Count : int
  member GetEnumerator : unit -> Enumerator<'T>
  member Peek : unit -> 'T
  member Pop : unit -> 'T
  member Push : item:'T -> unit
  member ToArray : unit -> 'T[]
  ...
  nested type Enumerator

--------------------
Stack() : Stack<'T>
Stack(capacity: int) : Stack<'T>
Stack(collection: IEnumerable<'T>) : Stack<'T>
val internal ofSeq : collection:IEnumerable<'T> -> Stack<'T>
val a : bool
type bool = System.Boolean
val internal contains : item:'T -> this:Stack<'T> -> bool
val b : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val internal count : this:Stack<'T> -> int
val c : string
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
val nums : ImmutableList<int>
(extension) IEnumerable.ToImmutableList<'TSource>() : ImmutableList<'TSource>
Multiple items
type ImmutableList =
  static member Create<'T> : unit -> ImmutableList<'T> + 2 overloads
  static member CreateBuilder<'T> : unit -> Builder<'T>
  static member CreateRange<'T> : items:IEnumerable<'T> -> ImmutableList<'T>
  static member IndexOf<'T> : list:IImmutableList<'T> * item:'T -> int + 3 overloads
  static member LastIndexOf<'T> : list:IImmutableList<'T> * item:'T -> int + 3 overloads
  static member Remove<'T> : list:IImmutableList<'T> * value:'T -> IImmutableList<'T>
  static member RemoveRange<'T> : list:IImmutableList<'T> * items:IEnumerable<'T> -> IImmutableList<'T>
  static member Replace<'T> : list:IImmutableList<'T> * oldValue:'T * newValue:'T -> IImmutableList<'T>
  static member ToImmutableList<'TSource> : source:IEnumerable<'TSource> -> ImmutableList<'TSource> + 1 overload

--------------------
type ImmutableList<'T> =
  member Add : value:'T -> ImmutableList<'T>
  member AddRange : items:IEnumerable<'T> -> ImmutableList<'T>
  member BinarySearch : item:'T -> int + 2 overloads
  member Clear : unit -> ImmutableList<'T>
  member Contains : value:'T -> bool
  member ConvertAll<'TOutput> : converter:Func<'T, 'TOutput> -> ImmutableList<'TOutput>
  member CopyTo : array:'T[] -> unit + 2 overloads
  member Count : int
  member Exists : match:Predicate<'T> -> bool
  member Find : match:Predicate<'T> -> 'T
  ...
  nested type Builder
  nested type Enumerator
active recognizer IsEmpty: ImmutableList<'T> -> unit option
val d : Stack<int>
val internal expr : Stack.StackBuilder
val num : int
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

--------------------
type seq<'T> = IEnumerable<'T>
val printfn : format:Printf.TextWriterFormat<'T> -> 'T