Skip to content

Commit

Permalink
Docs: Bang has other meaning for IO and RNG functions (JuliaLang#50138)
Browse files Browse the repository at this point in the history
* Docs: Bang has other meaning for IO and RNG functions

I have seen multiple people ask why functions such as `write` or `rand` does not
end with a `!`, given that they mutate the input IO or RNG object.
Mention in the style guide that `!` means something slightly different for
functions taking IO or RNG arguments.

* Update doc/src/manual/style-guide.md

Co-authored-by: Thomas Christensen <tchr@mit.edu>

* Update doc/src/manual/style-guide.md

Co-authored-by: Thomas Christensen <tchr@mit.edu>

* Update doc/src/manual/style-guide.md

Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>

* Increment -> advance

---------

Co-authored-by: Thomas Christensen <tchr@mit.edu>
Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
  • Loading branch information
3 people authored Jun 27, 2023
1 parent 76c906e commit fc8b700
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/src/manual/style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ with both copying and modifying forms (e.g., [`sort`](@ref) and [`sort!`](@ref))
which are just modifying (e.g., [`push!`](@ref), [`pop!`](@ref), [`splice!`](@ref)). It
is typical for such functions to also return the modified array for convenience.

Functions related to IO or making use of random number generators (RNG) are notable exceptions:
Since these functions almost invariably must mutate the IO or RNG, functions ending with `!` are used to signify a mutation _other_ than mutating the IO or advancing the RNG state.
For example, `rand(x)` mutates the RNG, whereas `rand!(x)` mutates both the RNG and `x`; similarly, `read(io)` mutates `io`, whereas `read!(io, x)` mutates both arguments.

## Avoid strange type `Union`s

Types such as `Union{Function,AbstractString}` are often a sign that some design could be cleaner.
Expand Down

0 comments on commit fc8b700

Please sign in to comment.