Skip to content

Commit 0b27900

Browse files
authoredDec 22, 2017
Document a convention for argument precedence (#25174)
This documents Jeff's proposed convention in #19150 in the manual's style guide. Fixes #19150
1 parent 5f14f11 commit 0b27900

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
 

‎doc/src/manual/style-guide.md

+60
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,66 @@ uses (e.g. `a[i]::Int`) than to try to pack many alternatives into one type.
164164
If a function name requires multiple words, consider whether it might represent more than one
165165
concept and might be better split into pieces.
166166

167+
## Write functions with argument ordering similar to Julia's Base
168+
169+
As a general rule, the Base library uses the following order of arguments to functions,
170+
as applicable:
171+
172+
1. **Function argument**.
173+
Putting a function argument first permits the use of [`do`](@ref) blocks for passing
174+
multiline anonymous functions.
175+
176+
2. **I/O stream**.
177+
Specifying the `IO` object first permits passing the function to functions such as
178+
[`sprint`](@ref), e.g. `sprint(show, x)`.
179+
180+
3. **Input being mutated**.
181+
For example, in [`fill!(x, v)`](@ref fill!), `x` is the object being mutated and it
182+
appears before the value to be inserted into `x`.
183+
184+
4. **Type**.
185+
Passing a type typically means that the output will have the given type.
186+
In [`parse(Int, "1")`](@ref parse), the type comes before the string to parse.
187+
There are many such examples where the type appears first, but it's useful to note that
188+
in [`read(io, String)`](@ref read), the `IO` argument appears before the type, which is
189+
in keeping with the order outlined here.
190+
191+
5. **Input not being mutated**.
192+
In `fill!(x, v)`, `v` is *not* being mutated and it comes after `x`.
193+
194+
6. **Key**.
195+
For associative collections, this is the key of the key-value pair(s).
196+
For other indexed collections, this is the index.
197+
198+
7. **Value**.
199+
For associative collections, this is the value of the key-value pair(s).
200+
In cases like `fill!(x, v)`, this is `v`.
201+
202+
8. **Everything else**.
203+
Any other arguments.
204+
205+
9. **Varargs**.
206+
This refers to arguments that can be listed indefinitely at the end of a function call.
207+
For example, in `Matrix{T}(uninitialized, dims)`, the dimensions can be given as a
208+
[`Tuple`](@ref), e.g. `Matrix{T}(uninitialized, (1,2))`, or as [`Vararg`](@ref)s,
209+
e.g. `Matrix{T}(uninitialized, 1, 2)`.
210+
211+
10. **Keyword arguments**.
212+
In Julia keyword arguments have to come last anyway in function definitions; they're
213+
listed here for the sake of completeness.
214+
215+
The vast majority of functions will not take every kind of argument listed above; the
216+
numbers merely denote the precedence that should be used for any applicable arguments
217+
to a function.
218+
219+
There are of course a few exceptions.
220+
For example, in [`convert`](@ref), the type should always come first.
221+
In [`setindex!`](@ref), the value comes before the indices so that the indices can be
222+
provided as varargs.
223+
224+
When designing APIs, adhering to this general order as much as possible is likely to give
225+
users of your functions a more consistent experience.
226+
167227
## Don't overuse try-catch
168228

169229
It is better to avoid errors than to rely on catching them.

0 commit comments

Comments
 (0)
Please sign in to comment.