From aa91752f212817c0fb5a4c0f7c4b93e771441450 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Wed, 3 Nov 2021 20:15:16 -0700 Subject: [PATCH 1/3] Add F# snippets for System.Action --- .../system.Action/fs/Action.fs | 21 ++++++++++++++++++ .../system.Action/fs/Action.fsproj | 16 ++++++++++++++ .../system.Action/fs/Delegate.fs | 21 ++++++++++++++++++ .../system.Action/fs/Lambda.fs | 22 +++++++++++++++++++ xml/System/Action.xml | 3 +++ 5 files changed, 83 insertions(+) create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs new file mode 100644 index 00000000000..87596e4b6c1 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs @@ -0,0 +1,21 @@ +module Action + +// +open System +open System.Windows.Forms + +type Name(name) = + let instanceName = name + + member _.DisplayToConsole() = + printfn "%s" instanceName + + member _.DisplayToWindow() = + MessageBox.Show instanceName |> ignore + +let testName = Name "Koani" +// unit -> unit functions and methods can be cast to Action. +let showMethod = Action testName.DisplayToWindow +showMethod.Invoke() + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fsproj new file mode 100644 index 00000000000..427dac272f0 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fsproj @@ -0,0 +1,16 @@ + + + + Exe + net5.0-windows + true + true + + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs new file mode 100644 index 00000000000..094fbf2be84 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs @@ -0,0 +1,21 @@ +module Delegate + +// +open System.Windows.Forms + +type ShowValue = delegate of unit -> unit + +type Name(name) = + let instanceName = name + + member _.DisplayToConsole() = + printfn "%s" instanceName + + member _.DisplayToWindow() = + MessageBox.Show instanceName |> ignore + +let testName = Name "Koani" +let showMethod = ShowValue testName.DisplayToWindow +showMethod.Invoke() + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs new file mode 100644 index 00000000000..2d72cf3ed21 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs @@ -0,0 +1,22 @@ +module Lambda + +// +open System +open System.Windows.Forms + +type Name(name) = + let instanceName = name + + member _.DisplayToConsole() = + printfn "%s" instanceName + + member _.DisplayToWindow() = + MessageBox.Show instanceName |> ignore + +let testName = Name "Koani" + +let showMethod = Action(fun () -> testName.DisplayToWindow()) + +showMethod.Invoke() + +// diff --git a/xml/System/Action.xml b/xml/System/Action.xml index 4340bc196eb..9f6390a7cdb 100644 --- a/xml/System/Action.xml +++ b/xml/System/Action.xml @@ -71,12 +71,14 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.action.delegate/cpp/delegate.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.action.delegate/cs/delegate.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.action.delegate/vb/delegate.vb" id="Snippet1"::: The following example simplifies this code by instantiating the delegate instead of explicitly defining a new delegate and assigning a named method to it. :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Action/cpp/action.cpp" id="Snippet2"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Action.cs" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action/vb/Action.vb" id="Snippet2"::: You can also use the delegate with anonymous methods in C#, as the following example illustrates. (For an introduction to anonymous methods, see [Anonymous Methods](/dotnet/csharp/programming-guide/statements-expressions-operators/anonymous-methods).) @@ -86,6 +88,7 @@ You can also assign a lambda expression to an delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Lambda.cs" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action/vb/lambda.vb" id="Snippet4"::: ]]> From b5228c05f49843a49857fc0235d33e2efc1dbb9c Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 12 Nov 2021 16:20:48 -0800 Subject: [PATCH 2/3] update snippets --- .../VS_Snippets_CLR_System/system.Action/fs/Action.fs | 8 ++++---- .../VS_Snippets_CLR_System/system.Action/fs/Delegate.fs | 8 ++++---- .../VS_Snippets_CLR_System/system.Action/fs/Lambda.fs | 6 ++---- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs index 87596e4b6c1..39a5fccaf9d 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs @@ -5,17 +5,17 @@ open System open System.Windows.Forms type Name(name) = - let instanceName = name - member _.DisplayToConsole() = - printfn "%s" instanceName + printfn "%s" name member _.DisplayToWindow() = - MessageBox.Show instanceName |> ignore + MessageBox.Show name |> ignore let testName = Name "Koani" + // unit -> unit functions and methods can be cast to Action. let showMethod = Action testName.DisplayToWindow + showMethod.Invoke() // diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs index 094fbf2be84..58afc2aff75 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs @@ -6,16 +6,16 @@ open System.Windows.Forms type ShowValue = delegate of unit -> unit type Name(name) = - let instanceName = name - member _.DisplayToConsole() = - printfn "%s" instanceName + printfn "%s" name member _.DisplayToWindow() = - MessageBox.Show instanceName |> ignore + MessageBox.Show name |> ignore let testName = Name "Koani" + let showMethod = ShowValue testName.DisplayToWindow + showMethod.Invoke() // diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs index 2d72cf3ed21..4937a44ed5b 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs @@ -5,13 +5,11 @@ open System open System.Windows.Forms type Name(name) = - let instanceName = name - member _.DisplayToConsole() = - printfn "%s" instanceName + printfn "%s" name member _.DisplayToWindow() = - MessageBox.Show instanceName |> ignore + MessageBox.Show name |> ignore let testName = Name "Koani" From 687206aae5b68f76ff41ddcd83eab37caf09c7b8 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 12 Nov 2021 17:07:16 -0800 Subject: [PATCH 3/3] Update Action.xml --- xml/System/Action.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xml/System/Action.xml b/xml/System/Action.xml index 9f6390a7cdb..96c6f047674 100644 --- a/xml/System/Action.xml +++ b/xml/System/Action.xml @@ -62,7 +62,7 @@ [!NOTE] > To reference a method that has no parameters and returns a value, use the generic delegate instead. @@ -85,7 +85,7 @@ :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Anon.cs" id="Snippet3"::: - You can also assign a lambda expression to an delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions).) + You can also assign a lambda expression to an delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions (C#)](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) or [Lambda Expressions (F#)](/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Lambda.cs" id="Snippet4"::: :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs" id="Snippet4":::