-
Notifications
You must be signed in to change notification settings - Fork 21
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
print and printn alongside printf and printfn #1092
Comments
let x = "Hello World"
printf x // Why is this not working!? can be written as let x = "Hello World"
printf $"{x}" |
@dulanov When we have the |
I like this suggestion. cc @KathleenDollard |
I didn't use old versions of F#, so I've in fact always used string interpolation instead of formatted strings 😄 , even when need to print a single object. Though |
|
I think it's worth a quick survey of some languages with a good learning curve and what all they do: Python
JavaScript
Go
Swift
I've excluded C/C++/Java/C# and don't think they're worth looking at. This suggestion would make F# the most like Go in this list:
I think that's the most reasonable, since changing this pattern will probably just get confusing to beginners once they get into formatted printing anyways. |
This is great. Currently to understand the typical beginning F# program |
Marking this as approved-in-principle. Since the design is simple by all means proceed straight to an RFC and PR :) |
I'd like us to consider
I could also see But it's a great idea, and other than thinking we should reconsider syntax, I love it! |
It feels like for consistency we have to use |
I see parallels between |
How about |
For comparison, I counted what other languages commonly use (see Rosetta for with and without newline): With newline (520 examples)
Some have other syntax, obviously, like Without newline (171 examples)
All in all, it appears that languages that have I know that we don't "have to do what other languages do" just for the sake of it, but for easy discovery, and considering that |
There should be no syntax for printing without a new line, as that just creates a pitfall for users. Printing without a new line is an very rare thing to want to do. Most of the time someone writes For printing (with a newline) I like |
Should |
Will |
For future reference, the final RFC 1125 is here, in case you missed it. And a PR is underway, see dotnet/fsharp#13597, great work @albert-du! |
Based on the PR discussion thread I've clarified a number of important unresolved issues regarding the PR - most notably whether the functions should be generic, and if so what should their spec be. @KevinRansom feels strongly that we should not add non-generic I think this puts the RFC and this suggestion on ice somewhat. |
@dsyme, I noticed that you reverted the RFC's function back from To follow the principle of least surprise, this would bring us to I'll certainly be able to live with |
@abelbraaksma Yes the consistency argument is too strong IMHO - I just can't justify another abbreviation for "Line" in FSharp.Core. People got used to In any case, the title of this suggestion was always |
Ok, fair enough. In the end there'll always be tooling that shows what we have once we start typing |
For reference, here's a good motivational example of how newcomers (still) struggle with this: https://stackoverflow.com/questions/73416468/hello-printfn-generates-an-error-in-f. |
I’m a fairly seasoned C# and python developer and tonight attempted my first foray into F# to potentially use as a tool to teach undergrad intro stats. I currently use either Python or R. Based on my research and initial trials it has huge potential over python. No virtual environments, easy to install, no anaconda, no silly dynamic typing. No silly However, I spent literally an hour trying to figure out why you had to do string formatting to print simple things. I even made a stackoverflow question before stumbling upon this thread. https://stackoverflow.com/q/73823075/4163879 I really support this thread. However, I really hope you all consider just making I can already picture 3 students coming to me after the first class in tears because they need to pass my class to continue their degree and their program isn’t working because Do the right thing. Anyone who wants to print without a new line will already have the skills required to Google it and figure out another function. F# has so much potential to beginners. Please make it easy on them. Add print, and make it add a new line. I implore you! 🙏 |
@AdamBebko Thank you for this feedback! |
For posterity (someone asked):
Afaik, there's currently no new work being done on this, though imo, there's still a strong want for an implementation here, so let's give this another chance. |
print and printn alongside printf and printfn
Currently, F#'s "Hello World" looks like this:
printf "Hello World"
We currently couple formatting with printing to the console, as introduction.
Problems with
printf
:Would surprise beginners, and we have to
Compare this with Python's
print("Hello World")
which has no f to understand.Moreover, we already have
failwith
alongsidefailwithf
, but noprint
alongsideprintf
.With string interpolation in F# 5, printf formatting should be considered the partial application friendly version of string interpolation when used with lambdas, like
function
is tofun x -> match x with
.I propose that we add two variations of
print
:print
andprintn
, asprintf
andprintfn
variants that take strings.Our "Hello World" is now as simple as Python's, even simpler without parentheses:
print "Hello World"
And this would work:
Pros and Cons
The advantages of making this adjustment to F# are
print "Hello World"
and avoid introducing printf formatters with the "f" in "printf" too early, rather going for interpolation first, and formatting later with partial applicationand prevent subtleties like
|> List.iter (printfn "%s")
vs|> List.iter print
The disadvantages of making this adjustment to F# is another way to do the same thing. However, it can make introductions to beginners easier. Without
print
, we can still usestdout.Write
andstdout.WriteLine
but with quirks related to inference of overloaded methods. Moreover, this also requires teaching the dot notation earlier.Extra information
Estimated cost (XS, S, M, L, XL, XXL): XS
Related suggestions:
(none)
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: