[erlang-questions] Dynamic pattern matching

David Holz <>
Tue Jan 15 01:01:13 CET 2008


I was pondering and jotting down some thoughts about dissecting the advantages out of OO-style method dispatch based on data type, and stumbled upon an interesting observation:

If the system allowed adding clauses to pattern matching operations at runtime, we'd have Lisp multimethods:

-module(bar).
[generic function tweak/2].

-module(foo).
[add clause]bar:tweak({add,{foo_data,X}}, Data) -> ...

-module(baz).
[add clause]bar:tweak({add,{baz_data,X}}, Data) -> ...

This would need to have some method of handling pattern precedence instead of relying on ordering as currently done, and what happens during code reload would need to be thought through (probably track and remove a module's previously added clauses when a change happens, then re-add the new ones).  This also could apply just as well to receive handlers (yay, inherit/extend messaging functionality! (maybe!)) and any other pattern matching places.  Also, I don't know of any function that gets called when a module is loaded, but doing multimethod clauses additions could really use those so they could be called either at module load/change time or at any point during runtime.

I also rediscovered the idea of -extend(Mod). as I see has already been presented at EUC'07 after googling it, but with a sort of "handle in ?BASE_MODULE" shortcut instead of having to write a final tweak(A,B,C,D,E) -> BASE:tweak(A,B,C,D,E). clause when you only want to extend and not replace a function.

Just something to spark new ideas as Erlang moves forward.

_________________________________________________________________
Get the power of Windows + Web with the new Windows Live.
http://www.windowslive.com?ocid=TXT_TAGHM_Wave2_powerofwindows_012008


More information about the erlang-questions mailing list