I have a function that accepts coordinates (tuple) as one of its arguments:
func({X, Y}, Something) when is_integer(X), is_integer(Y) -> ...
I want to ensure that the coordinates:
- are tuple with 2 items (X and Y)
- both X and Y are integers
I can use the guard as above, and it works all right. But, I have many functions that use the coordinates and I wanted to know if I can clean up somehow this construct (some macro?) so would have something like:
func(XY, Something) when ?is_coord(XY) -> ... % how to define ?is_coord
Is there a clean and idiomatic way to do that? Is it erlang-ish?
Edit
Erlang docs explicitly discourage defensive programming:
3.13 Do not program "defensively"
A defensive program is one where the programmer does not "trust" the input data to the part of the system they are programming. In general one should not test input data to functions for correctness. Most of the code in the system should be written with the assumption that the input data to the function in question is correct. Only a small part of the code should actually perform any checking of the data. This is usually done when data "enters" the system for the first time, once data has been checked as it enters the system it should thereafter be assumed correct.