elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.52k stars 3.38k forks source link

Support multimethods (discussion on configurability) #192

Closed Raynes closed 12 years ago

Raynes commented 12 years ago

A good example of multimethods is Clojure's multimethods. I think Elixir could benefit from them significantly. I have a scenario in particular in mind that I'd be able to use them for.

I'm working on a URI parser. There are many different schemes that have scheme-specific functionality. For example, the default port for http is 80 while the default port for https is 443. I'd like to make scheme-specific parsing behavior extensible so that users can add their own schemes and parsing behavior on top of my generic parsing. Right now, I allow for an Orddict of schemes -> functions that take a generic-parsed URI and do whatever they want with it and return it. I have a default list for the major schemes. This is a bit ugly and is exactly what multimethods are designed for.

josevalim commented 12 years ago

I will just reproduce the arguments I have said on irc for documentation purposes:

This is complicated to implement in Elixir because unfortunately we cannot easily attach a new method to a multimethod definition. The only way we could do this would be to compile a new module for each method (similar to our protocols) which seems a bit excessive at first.

The current URI parser problem was solved by making reference lookups based on the protocol name. It is a very simple solution and extensible, however, it may allow atom injection.

In any case, this problem will appear many times and we need to discuss how we can address such configuration styles.

josevalim commented 12 years ago

I am closing this for now (doing some issues tracker pruning). If people ask for it once again (and once again and once again) it is a good indicator we need to improve this area. :)

al6x commented 7 years ago

Any news on this? :)