[erlang-questions] Behavio(u)r guard?

Andrew Thompson andrew@REDACTED
Fri Apr 15 22:02:56 CEST 2011


On Fri, Apr 15, 2011 at 12:37:43PM -0700, Mike Oxford wrote:
> I do not see any way to specify a behavior as a guard.
> For a callback module, I cannot then say "if it's this kind of handler, ship
> it over this way."
> 
> This is not critical or anything, but from a pure curiosity standpoint is
> there an underlying reason, like there
> is for not having is_string()?  Is the behavior/interface too soft to check
> as a guard?
> 

This is because behaviours are compile-time, not run-time. The compiler
just asks the behaviour that the module claims it implements what
functions must be exported and checks that against the export list (and
warns on any missing ones).

At runtime you can erlang:function_exported(M, F, A) to check if a
function is exported from a particular module. This is useful for
optional callbacks that a behaviour *may* implement (eg.
gen_server:format_status).

Andrew



More information about the erlang-questions mailing list