rfc:println
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:println [2021/03/13 18:46] – created tandre | rfc:println [2021/03/13 20:03] – tandre | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2021-03-13 | * Date: 2021-03-13 | ||
* Author: Tyson Andre | * Author: Tyson Andre | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
+ | * Implementation: | ||
===== Introduction ===== | ===== Introduction ===== | ||
Printing a string followed by a newline to stdout is a commonly performed operation in many applications. Many programming languages provide a helper function to do this specifically, | Printing a string followed by a newline to stdout is a commonly performed operation in many applications. Many programming languages provide a helper function to do this specifically, | ||
- | - Java has '' | + | |
- | - Python '' | + | - Python '' |
- | - C has '' | + | - C has '' |
- | - Golang has '' | + | - Golang has '' |
- | - Rust has '' | + | - Rust has '' |
- | - And so on. | + | - And so on. |
However, PHP currently does not yet have a standalone helper method to do this. | However, PHP currently does not yet have a standalone helper method to do this. | ||
Line 23: | Line 24: | ||
The behavior is equivalent to the following polyfill but expected to be more efficient due to avoiding concatenation. | The behavior is equivalent to the following polyfill but expected to be more efficient due to avoiding concatenation. | ||
- | Similarly to https:// | + | Similarly to https:// |
Line 98: | Line 99: | ||
'' | '' | ||
- | * Depending on the application, | + | Depending on the application, |
+ | |||
+ | ''< | ||
- | '' | ||
(or other alternatives such as '' | (or other alternatives such as '' | ||
Line 110: | Line 112: | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
- | Other printing functions or statements are unaffected. '' | + | Other printing functions or statements are unaffected. |
- | ===== Future Scope ===== | + | '' |
- | This section details areas where the feature | + | |
+ | ===== Discussion | ||
+ | |||
+ | ==== This could go in a Composer/ | ||
+ | |||
+ | https:// | ||
+ | |||
+ | < | ||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | function println(string $x): void { | ||
+ | echo $x, PHP_EOL; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | I hereby grant a public domain license to the above code and wish you | ||
+ | godspeed bundling it into a composer package to be enjoyed by users of | ||
+ | every active version of PHP. | ||
+ | |||
+ | -Sara | ||
+ | </ | ||
+ | |||
+ | In practice, I haven' | ||
+ | Additionally, | ||
+ | |||
+ | A new contributor to a project | ||
+ | |||
+ | If multiple composer packages were published and declared println (in the global namespace) with different behaviors, that would lead to confusion and bugs, which could be avoided by declaring println in PHP itself. | ||
+ | |||
+ | ==== This may not be commonly used for HTML ==== | ||
+ | |||
+ | https:// | ||
+ | |||
+ | < | ||
+ | > 3. Add a new method, perhaps " | ||
+ | |||
+ | Of the suggestions put forward, this is the only one I can see having | ||
+ | any chance of succeeding. | ||
+ | |||
+ | However, I think the big reason this doesn' | ||
+ | been touched on by other responses: PHP started as, and is still | ||
+ | primarily regarded as, a language for building websites. In that | ||
+ | context, newline characters are generally considered " | ||
+ | whitespace"; | ||
+ | want "< | ||
+ | into the tricky topic of escaping. | ||
+ | |||
+ | Even in CLI scripts, as soon as you're building anything intended for | ||
+ | reuse, you're likely to write a function like log_string() which adds | ||
+ | information like timestamp, category, severity. The use cases for a new | ||
+ | function / keyword may therefore be rather limited. | ||
+ | |||
+ | Regards, | ||
+ | |||
+ | -- | ||
+ | Rowan Collins | ||
+ | [IMSoP] | ||
+ | </ | ||
+ | |||
+ | Some CLI scripts would use a specialized helper, but not all of them, and a helper may be used in some places but not others. Some CLI scripts | ||
+ | |||
+ | Even when used as a web server, PHP would also serve resources with non-HTML content types such as '' | ||
+ | Within HTML, there are elements such as ''< | ||
+ | |||
+ | ==== Choice of println for the name ==== | ||
+ | |||
+ | Naming was discussed in a different user's proposal to add a " | ||
+ | |||
+ | < | ||
+ | nikic commented on 14 Jun 2019 | ||
+ | |||
+ | > Inspiration from Rust: https:// | ||
+ | |||
+ | Speaking of ... I think it would make a lot more sense to call this function println rather than puts. The fact that puts() is like print but with a newline will not be obvious to anyone without a C background. (Or even to someone with a C background for that matter, I haven' | ||
+ | </ | ||
+ | |||
+ | As stated | ||
+ | Additionally, | ||
+ | |||
+ | (e.g. changing '' | ||
+ | |||
+ | ==== Choice of Unix newline(\n) ==== | ||
+ | |||
+ | Some programming languages such as Java use a platform-specific or configurable newline https:// | ||
+ | Others, such as rust and golang, always use a unix newline. | ||
+ | |||
+ | There have been some comments in favor of Unix newlines, and other comments in favor of PHP_EOL or configurable settings - e.g. https:// | ||
+ | |||
+ | My reasons for unconditionally choosing the Unix newline are documented in the section [[# | ||
+ | |||
+ | ==== Should | ||
+ | |||
+ | |||
+ | https:// | ||
+ | |||
+ | < | ||
+ | Should' | ||
+ | |||
+ | It would feel a little weird being able to do: | ||
+ | |||
+ | <code php> | ||
+ | echo 'hello world'; | ||
+ | print 'hello world'; | ||
+ | </ | ||
+ | ... but not: | ||
+ | |||
+ | <code php> | ||
+ | puts 'hello world'; | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Adding this as a language construct (i.e. new statement type) is something I'd expect to be contentious, | ||
+ | |||
+ | - Cause a larger BC break. | ||
+ | - Increase the complexity of the language specification and make the language a bit harder to learn; I'd prefer simplicity by using an ordinary global function in this case. | ||
+ | - Be impossible to polyfill (when brackets weren' | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Yes/No, requiring 2/3 majority. | + | Yes/No, requiring |
===== References ===== | ===== References ===== | ||
https:// | https:// | ||
- | ===== Rejected Features ===== | + | https:// |
- | + | ||
- | ==== Alternate names ==== | + |
rfc/println.txt · Last modified: 2021/03/15 13:26 by tandre