From e514800e31aba652dd0930b4d6f8eb892a1f20bc Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Wed, 3 Nov 2021 22:10:12 -0700 Subject: [PATCH 1/2] Add System.Action F# Snippets --- .../system.Action~2/fs/Action2.fs | 29 +++++++++++++++++ .../system.Action~2/fs/Action2.fsproj | 13 ++++++++ .../system.Action~2/fs/Delegate.fs | 31 +++++++++++++++++++ .../system.Action~2/fs/Lambda.fs | 29 +++++++++++++++++ xml/System/Action`2.xml | 7 +++-- 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs new file mode 100644 index 00000000000..ae7a696d402 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs @@ -0,0 +1,29 @@ +module TestLambdaExpression + +// +open System +open System.IO + +let message1 = "The first line of a message" +let message2 = "The second line of a message" + +let writeToConsole string1 string2 = + printfn $"{string1}\n{string2}" + +let writeToFile string1 string2 = + use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) + try + writer.WriteLine $"{string1}\n{string2}" + with _ -> printfn "File write operation failed..." + +let concat = + Action( + if Environment.GetCommandLineArgs().Length > 1 then + writeToFile + else + writeToConsole + ) + +concat.Invoke(message1, message2) + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fsproj new file mode 100644 index 00000000000..68850b7e7c3 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fsproj @@ -0,0 +1,13 @@ + + + + Exe + net5.0 + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs new file mode 100644 index 00000000000..e11e7f4b9d2 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs @@ -0,0 +1,31 @@ +module TestDelegate + +// +open System +open System.IO + +type ConcatStrings = delegate of string1: string * string1: string -> unit + +let message1 = "The first line of a message" +let message2 = "The second line of a message" + +let writeToConsole string1 string2 = + printfn $"{string1}\n{string2}" + +let writeToFile string1 string2 = + use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) + try + writer.WriteLine $"{string1}\n{string2}" + with _ -> printfn "File write operation failed..." + +let concat = + ConcatStrings( + if Environment.GetCommandLineArgs().Length > 1 then + writeToFile + else + writeToConsole + ) + +concat.Invoke(message1, message2) + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs new file mode 100644 index 00000000000..62556231803 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs @@ -0,0 +1,29 @@ +module TestDelegate + +// +open System +open System.IO + +let message1 = "The first line of a message" +let message2 = "The second line of a message" + +let writeToConsole string1 string2 = + printfn $"{string1}\n{string2}" + +let writeToFile string1 string2 = + use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) + try + writer.WriteLine $"{string1}\n{string2}" + with _ -> printfn "File write operation failed..." + +let concat = + Action( + if Environment.GetCommandLineArgs().Length > 1 then + fun s1 s2 -> writeToFile s1 s2 + else + fun s1 s2 -> writeToConsole s1 s2 + ) + +concat.Invoke(message1, message2) + +// diff --git a/xml/System/Action`2.xml b/xml/System/Action`2.xml index deb691613c0..4a485cf08b5 100644 --- a/xml/System/Action`2.xml +++ b/xml/System/Action`2.xml @@ -84,7 +84,7 @@ delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have two parameters that are both passed to it by value, and it must not return a value. (In C#, the method must return `void`. In Visual Basic, it must be defined by the `Sub`…`End Sub` construct. It can also be a method that returns a value that is ignored.) Typically, such a method is used to perform an operation. + You can use the delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have two parameters that are both passed to it by value, and it must not return a value. (In C#, the method must return `void`. In F#, the method or function must return unit. In Visual Basic, it must be defined by the `Sub`…`End Sub` construct. It can also be a method that returns a value that is ignored.) Typically, such a method is used to perform an operation. > [!NOTE] > To reference a method that has two parameters and returns a value, use the generic delegate instead. @@ -92,20 +92,23 @@ When you use the delegate, you do not have to explicitly define a delegate that encapsulates a method with two parameters. For example, the following code explicitly declares a delegate named `ConcatStrings`. It then assigns a reference to either of two methods to its delegate instance. One method writes two strings to the console; the second writes two strings to a file. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/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="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Action2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/action2.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).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Anon.cs" interactive="try-dotnet" 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~2/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/lambda.vb" id="Snippet4"::: ]]> From c34a82bd5ed7cdd112a9be81554d877f04def16b Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 19 Nov 2021 06:54:16 -0800 Subject: [PATCH 2/2] update snippets Co-Authored-By: Don Syme --- .../system.Action~2/fs/Action2.fs | 10 ++++------ .../system.Action~2/fs/Delegate.fs | 10 ++++------ .../system.Action~2/fs/Lambda.fs | 4 +--- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs index ae7a696d402..ef1f576a0cf 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs @@ -12,16 +12,14 @@ let writeToConsole string1 string2 = let writeToFile string1 string2 = use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) - try - writer.WriteLine $"{string1}\n{string2}" - with _ -> printfn "File write operation failed..." + writer.WriteLine $"{string1}\n{string2}" let concat = - Action( + Action(fun string1 string2 -> if Environment.GetCommandLineArgs().Length > 1 then - writeToFile + writeToFile string1 string2 else - writeToConsole + writeToConsole string1 string2 ) concat.Invoke(message1, message2) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs index e11e7f4b9d2..ecedcc7c4b8 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs @@ -14,16 +14,14 @@ let writeToConsole string1 string2 = let writeToFile string1 string2 = use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) - try - writer.WriteLine $"{string1}\n{string2}" - with _ -> printfn "File write operation failed..." + writer.WriteLine $"{string1}\n{string2}" let concat = - ConcatStrings( + ConcatStrings(fun string1 string2 -> if Environment.GetCommandLineArgs().Length > 1 then - writeToFile + writeToFile string1 string2 else - writeToConsole + writeToConsole string1 string2 ) concat.Invoke(message1, message2) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs index 62556231803..e3b9053b647 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs @@ -12,9 +12,7 @@ let writeToConsole string1 string2 = let writeToFile string1 string2 = use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) - try - writer.WriteLine $"{string1}\n{string2}" - with _ -> printfn "File write operation failed..." + writer.WriteLine $"{string1}\n{string2}" let concat = Action(