-
Notifications
You must be signed in to change notification settings - Fork 864
Add generic print and println functions to FSharp.Core #19265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bbatsov
wants to merge
4
commits into
dotnet:main
Choose a base branch
from
bbatsov:feature/print-println
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c1389a
Add generic print and printn functions to FSharp.Core
bbatsov c3f34de
Add IL baseline tests for print and printn
bbatsov badbee1
Add static optimizations for string, char, and bool
bbatsov 0f9b3ba
Move print/printn release notes to FSharp.Core 11.0.100
bbatsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
tests/FSharp.Compiler.ComponentTests/EmittedIL/PrintFunction.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| namespace EmittedIL | ||
|
|
||
| open Xunit | ||
| open FSharp.Test.Compiler | ||
|
|
||
| module PrintFunction = | ||
|
|
||
| [<Fact>] | ||
| let ``print with int specializes to Int32 ToString with InvariantCulture``() = | ||
| FSharp """ | ||
| module PrintInt | ||
|
|
||
| let printInt () = print 42 | ||
| """ | ||
| |> withOptimize | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> verifyIL [ | ||
| """call class [netstandard]System.IO.TextWriter [netstandard]System.Console::get_Out()""" | ||
| """call class [netstandard]System.Globalization.CultureInfo [netstandard]System.Globalization.CultureInfo::get_InvariantCulture()""" | ||
| """call instance string [netstandard]System.Int32::ToString(string,""" | ||
| """callvirt instance void [netstandard]System.IO.TextWriter::Write(string)"""] | ||
|
|
||
| [<Fact>] | ||
| let ``printn with int specializes to Int32 ToString with InvariantCulture``() = | ||
| FSharp """ | ||
| module PrintnInt | ||
|
|
||
| let printnInt () = printn 42 | ||
| """ | ||
| |> withOptimize | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> verifyIL [ | ||
| """call class [netstandard]System.IO.TextWriter [netstandard]System.Console::get_Out()""" | ||
| """call class [netstandard]System.Globalization.CultureInfo [netstandard]System.Globalization.CultureInfo::get_InvariantCulture()""" | ||
| """call instance string [netstandard]System.Int32::ToString(string,""" | ||
| """callvirt instance void [netstandard]System.IO.TextWriter::WriteLine(string)"""] | ||
|
|
||
| [<Fact>] | ||
| let ``print with string calls Write directly``() = | ||
| FSharp """ | ||
| module PrintString | ||
|
|
||
| let printStr () = print "hello" | ||
| """ | ||
| |> withOptimize | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> verifyIL [ | ||
| """call class [netstandard]System.IO.TextWriter [netstandard]System.Console::get_Out()""" | ||
| """callvirt instance void [netstandard]System.IO.TextWriter::Write(string)"""] | ||
|
|
||
| [<Fact>] | ||
| let ``print with char calls Write char overload``() = | ||
| FSharp """ | ||
| module PrintChar | ||
|
|
||
| let printChar () = print 'A' | ||
| """ | ||
| |> withOptimize | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> verifyIL [ | ||
| """call class [netstandard]System.IO.TextWriter [netstandard]System.Console::get_Out()""" | ||
| """callvirt instance void [netstandard]System.IO.TextWriter::Write(char)"""] | ||
|
|
||
| [<Fact>] | ||
| let ``print with bool calls Write bool overload``() = | ||
| FSharp """ | ||
| module PrintBool | ||
|
|
||
| let printBool () = print true | ||
| """ | ||
| |> withOptimize | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> verifyIL [ | ||
| """call class [netstandard]System.IO.TextWriter [netstandard]System.Console::get_Out()""" | ||
| """callvirt instance void [netstandard]System.IO.TextWriter::Write(bool)"""] | ||
|
|
||
| [<Fact>] | ||
| let ``printn with float specializes to Double ToString with InvariantCulture``() = | ||
| FSharp """ | ||
| module PrintnFloat | ||
|
|
||
| let printnFloat () = printn 3.14 | ||
| """ | ||
| |> withOptimize | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> verifyIL [ | ||
| """call class [netstandard]System.IO.TextWriter [netstandard]System.Console::get_Out()""" | ||
| """call class [netstandard]System.Globalization.CultureInfo [netstandard]System.Globalization.CultureInfo::get_InvariantCulture()""" | ||
| """call instance string [netstandard]System.Double::ToString(string,""" | ||
| """callvirt instance void [netstandard]System.IO.TextWriter::WriteLine(string)"""] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Core/PrintTests.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| // Various tests for: | ||
| // Microsoft.FSharp.Core.ExtraTopLevelOperators.print | ||
| // Microsoft.FSharp.Core.ExtraTopLevelOperators.printn | ||
|
|
||
| namespace FSharp.Core.UnitTests | ||
|
|
||
| open System | ||
| open System.IO | ||
| open Xunit | ||
|
|
||
| [<Collection(nameof FSharp.Test.NotThreadSafeResourceCollection)>] | ||
| type PrintTests() = | ||
|
|
||
| let captureConsoleOut (f: unit -> unit) = | ||
| let oldOut = Console.Out | ||
| use sw = new StringWriter() | ||
| Console.SetOut(sw) | ||
| try | ||
| f () | ||
| sw.ToString() | ||
| finally | ||
| Console.SetOut(oldOut) | ||
|
|
||
| [<Fact>] | ||
| member _.``print writes string value``() = | ||
| let result = captureConsoleOut (fun () -> print "hello") | ||
| Assert.Equal("hello", result) | ||
|
|
||
| [<Fact>] | ||
| member _.``print writes integer value``() = | ||
| let result = captureConsoleOut (fun () -> print 42) | ||
| Assert.Equal("42", result) | ||
|
|
||
| [<Fact>] | ||
| member _.``print writes float with InvariantCulture``() = | ||
| let result = captureConsoleOut (fun () -> print 3.14) | ||
| Assert.Equal("3.14", result) | ||
|
|
||
| [<Fact>] | ||
| member _.``print writes bool value``() = | ||
| let result = captureConsoleOut (fun () -> print true) | ||
| Assert.Equal("True", result) | ||
|
|
||
| [<Fact>] | ||
| member _.``print writes Some value``() = | ||
| let result = captureConsoleOut (fun () -> print (Some 42)) | ||
| Assert.Equal("Some(42)", result) | ||
|
|
||
| [<Fact>] | ||
| member _.``print writes None value``() = | ||
| let result = captureConsoleOut (fun () -> print None) | ||
| Assert.Equal("", result) | ||
|
|
||
| [<Fact>] | ||
| member _.``print writes list value``() = | ||
| let result = captureConsoleOut (fun () -> print [1; 2; 3]) | ||
| Assert.Equal("[1; 2; 3]", result) | ||
|
|
||
| [<Fact>] | ||
| member _.``printn writes value followed by newline``() = | ||
| let result = captureConsoleOut (fun () -> printn "hello") | ||
| Assert.Equal("hello" + Environment.NewLine, result) | ||
|
|
||
| [<Fact>] | ||
| member _.``multiple prints concatenate``() = | ||
| let result = captureConsoleOut (fun () -> | ||
| print "Hello, " | ||
| print "World!") | ||
| Assert.Equal("Hello, World!", result) | ||
|
|
||
| [<Fact>] | ||
| member _.``printn writes integer with newline``() = | ||
| let result = captureConsoleOut (fun () -> printn 42) | ||
| Assert.Equal("42" + Environment.NewLine, result) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.