I am writing some module in Erlang that is supposed to be compiled on various OTP versions. In some cases I would like to use the unsupported in older versions ++ operator pattern, but only if it is available. Therefore my attempt to this task goes as follows:
f([]) -> empty;
f([_|_]) -> cons;
-if(?OTP_RELEASE >= 23).
f([] ++ _) -> empty_append;
-endif.
f(X) -> error(nah).
The workarounds to this issue I found are:
Move the iffed clause to the beginning. This however does not work well when:
- I need to add more
-ifs - The order of clauses forbids me from doing so for any reason.
- I need to add more
Create a beautiful garden of ifelses with whole function copied several times. Obviously, this does not pass.
Is there some convenient way to do it? If possible, I'd appreciate a general solution, not limited to the presented case.