January 2020
Intermediate to advanced
532 pages
13h 31m
English
A drawback of optional arguments is that they must be in the same order in which they are defined. When there are more arguments, it is not easily readable which values are bound to which arguments from the call site. In that case, we may use keyword arguments to improve readability.
Let's redefine the make_asteroid function as follows:
function make_asteroids2(N::Int; pos_range = 0:200, size_range = 10:30) pos_rand() = rand(pos_range) sz_rand() = rand(size_range) return [Widget("Asteroid #$i", Position(pos_rand(), pos_rand()), Size(sz_rand(), sz_rand())) for i in 1:N]end
The only difference between this function and the one from the previous section is just a single character. The positional arguments (in this ...
Read now
Unlock full access