Add generic print and println functions to FSharp.Core#19265
Conversation
❗ Release notes requiredYou can open this PR in browser to add release notes: open in github.dev
|
53c25f7 to
c00b32e
Compare
|
Will also probably want IL baseline tests to make sure correct overloads are getting called. |
|
I've added some IL tests - hopefully I got those right. First contributions as always the hardest... 😅 |
Yeah, those are the ones I meant. However, I do see that for some of them it is calling ToString for some value types (i32 for example), and calls string overload for the Write(Line) |
That's inherent to the approach taken in this PR (calling We could delegate to the appropriate overload of |
Yes, I forgot to look at the implementation tbh.
We probably should, jit might lift some allocations to stack, but we shouldn't rely on it. |
|
I think I'll need some more pointers about the next step, as I'm not quite sure how to proceed from here. |
|
@bbatsov consider looking at the implementation for the string function and use a similar syntax for Console.WriteLine overloads. |
|
I've added static optimizations for For numeric types ( The IL tests now verify:
I hope I got those right. I never thought a couple of print functions could be so involved. 😅 |
485c8dc to
43b247f
Compare
Head branch was pushed to by a user without write access
2db7c71 to
5833768
Compare
|
In case that matters - I rebased the PR on top of the current |
|
Release notes for 11 (it will now be more stable for requiring 11 for about half a year). |
|
Just moved the release notes to (hopefully) the right place. |
Head branch was pushed to by a user without write access
11ad62e to
72c8b44
Compare
|
The RFC describes |
|
@esbenbjerre Good point! I went with |
|
I agree go with the RFC |
| /// </code> | ||
| /// </example> | ||
| [<CompiledName("PrintValueLine")>] | ||
| val inline println: value: 'T -> unit |
There was a problem hiding this comment.
(doing this to prevent accidental merging before renaming and getting rid of the l. Thanks everyone for being vigilant 👍 )
|
Now that we're aligned on the naming I'll update the PR soon. |
Add inline `print: 'T -> unit` and `printn: 'T -> unit` to ExtraTopLevelOperators. These use the existing `string` function for conversion (InvariantCulture for IFormattable, .ToString() fallback) and write to Console.Out. RFC FS-1125
Verify that inline specialization produces direct calls to Int32.ToString, Double.ToString with InvariantCulture, and Console.Out.Write/WriteLine for the respective types.
72c8b44 to
1344cb9
Compare
|
Renamed |
|
Just run fantomas now and it is ready to go 👍 |
For culture-independent types (string, char, bool), bypass the `string` operator and call the appropriate TextWriter.Write/WriteLine overload directly. Numeric types (int, float, etc.) must still go through `string` to ensure InvariantCulture formatting, since TextWriter.Write(int/float) uses the writer's FormatProvider which is CurrentCulture for Console.Out. Add IL tests verifying char and bool use their direct overloads.
1344cb9 to
0f9b3ba
Compare
|
Build's green now. The formatting failure wasn't actually about the rename - it was the static-optimization |
Summary
This is another take on RFC FS-1125, following up on the closed #13597. I guess that's mostly an attempt to see if anyone's interested in driving the old proposal to the finish line. For me those would definitely be handy functions to have, but obviously they are not something very important.
The key difference from the original PR is that
printandprintlnare generic ('T -> unit) rather thanstring -> unit, addressing the feedback from @dsyme that a generic print function would be a better use of the "good name":Both functions use the existing
stringoperator for conversion, which already:IFormattabletypes (consistent withprintfn,sprintf, etc.).ToString()for other types (F# types likeOption,List, etc. have good.ToString()overrides)Changes
printandprintlnsignatures with XML docs tofslib-extra-pervasives.fsifslib-extra-pervasives.fsPrintTests.fs) covering: string, int, float, bool, Option, None, list, newline behavior, and concatenationOpen questions
eprintf(n)andfprintf(n)? Do they generic counterparts as well?