fsharp / fsharp/fslang-suggestions
print and printn alongside printf and printfn
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
# 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`:
- It forces the notion of "f" - formatting, and all the variations on formatting specifiers
- It does not take string, so
```fs
let x = "Hello world"
printf x
```
Would surprise beginners, and we have to
```fs
let x = "Hello world"
printf "%s" x
```
- For formatting we should first introduce string interpolation anyways, since that understanding of formatting specifiers for it is optional.
Compare this with Python's `print("Hello World")` which has no f to understand.
Moreover, we already have `failwith` alongside `failwithf`, but no `print` alongside `printf`.
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 to `fun x -> match x with`.
```fs
List.iter (printfn "Here is an integer: %d") [1..9]
List.iter (fun i -> printn $"Here is an integer: {i}") [1..9]
```
I propose that we add two variations of `print`: `print` and `printn`, as `printf` and `printfn` variants that take strings.
Our "Hello World" is now as simple as Python's, even simpler without parentheses:
```fs
print "Hello World"
```
And this would work:
```fs
["String 1"; "String 2"; "String 3"]
|> List.iter print
```
## Pros and Cons
The advantages of making this adjustment to F# are
1. We can make a good impression with a simple `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 application
2. Have an actual function that can print messages without adding an unnecessary %s
and prevent subtleties like
```fs
let x = "Hello World"
printf x // Why is this not working!?
```
3. Easier piping with pre-interpolated strings, `|> 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 use `stdout.Write` and `stdout.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:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reviewing the proposal's relationship between printf/printfn, failwith/failwithf, string interpolation, and List.iter, then read the 26-comment discussion for the current design status. Done would mean an agreed implementation of print and printn alongside printf and printfn, including the Hello World and List.iter examples described here.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100