2.0 KiB
2.0 KiB
Hints
General
- Read about structs in the Getting Started guide.
- Read about
defstruct
in the documentation. - Watch Elixir Casts - #106: Intro to Structs.
1. Create a brand-new remote controlled car
- The module attribute
@enforce_keys
can be used to specify required values. It needs to be defined before callingdefstruct
. - The
nickname
field should not have a default value specified. - The
new/0
function should initialize thenickname
with the value"none"
.
2. Create a brand-new remote controlled car with a nickname
- Use
multiple-function-clauses
to reuse the function name but accept different arguments. - Consider a
default-argument
for the function.
3. Display the distance
- For functions which accept only a specific type of struct, make sure you perform a pattern match to check the argument.
- Use the static access operator to obtain field values.
4. Display the battery percentage
- For functions which accept only a specific type of struct, make sure you perform a pattern match to check the argument.
- Use the static access operator to obtain field values.
5. Driving changes the battery and distance driven
- For functions which accept only a specific type of struct, make sure you perform a pattern match to check the argument.
- Use the static access operator to obtain field values.
- Review the introduction for updating structs.
6. Account for driving with a dead battery
- If the battery is dead, it should return the struct unchanged.