January 2020
Intermediate to advanced
532 pages
13h 31m
English
Occasionally, you may encounter a situation where you need to determine whether a data type implements an interface. The information about whether a data type exhibits certain behavior is also called a trait.
How do we implement traits for an interface? In the Vehicle module, we can add a new function, as follows:
# traithas_wheels(vehicle) = error("Not implemented.")
This default implementation simply raises an error, and that's intentional. This trait function is expected to be implemented by any vehicle data types. In the interface code, the landing function can make use of the trait function for a more refined logic:
# Landing (using trait)function land2!(vehicle) has_wheels(vehicle) && engage_wheels!(vehicle)
Read now
Unlock full access