Add generic print and println functions to FSharp.Core#19265
Add generic print and println functions to FSharp.Core#19265bbatsov wants to merge 2 commits intodotnet:mainfrom
Conversation
❗ Release notes required
|
| [<CompiledName("PrintValue")>] | ||
| let print (value: 'T) = | ||
| Console.Out.Write(string value) | ||
|
|
||
| [<CompiledName("PrintValueLine")>] | ||
| let println (value: 'T) = | ||
| Console.Out.WriteLine(string value) | ||
|
|
There was a problem hiding this comment.
Should functions be inline?
There was a problem hiding this comment.
Yes, @bbatsov pls mark at as inline so that it can specialize around the string call for certain types and avoid a .ToString() call.
Add inline `print: 'T -> unit` and `println: '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
53c25f7 to
c00b32e
Compare
|
Will also probably want IL baseline tests to make sure correct overloads are getting called. |
Verify that inline specialization produces direct calls to Int32.ToString, Double.ToString with InvariantCulture, and Console.Out.Write/WriteLine for the respective types.
|
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. |
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?