January 2020
Intermediate to advanced
532 pages
13h 31m
English
Sometimes, it is more convenient if the function can just accept any number of arguments. In this case, we can add three dots ... to a function argument and Julia will automatically roll up all passed arguments into a single variable. This feature is known as slurping.
Here is an example:
# Shoot any number of targetsfunction shoot(from::Widget, targets::Widget...) println("Type of targets: ", typeof(targets)) for target in targets println(from.name, " --> ", target.name) endend
In the shoot function, we first print the type of the targets variable and then print every shot that was fired. Let's set up the game pieces first:
spaceship = Widget("Spaceship", Position(0, 0), Size(30,30))target1 = asteroids[1] ...Read now
Unlock full access