Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Fabio's approach does the job, but it fails at genericity - its usefulness is limited to the scope of the Formotion project. Which is unfortunate because something actually simple is going on - a mapping of keywords to functions.

What you really want is multimethods - they provide all the goodness of classic polymorphism, without forcing you to model intrincate, rigid class hierarchies/relationships.

Some pseudocode illustrating the concept:

    # declaration. the 'dispatch function' we pass can build an arbitrary value out of the args it receives.
    defmulti build_cell lambda { |args| args[1].tag }
    
    # if the value the dispach function generates equals :submit, this body is gonna be called
    defmethod build_cell :submit do
        # implementation...
    end
    
    # more defmethods for :switch, :check, :edit etc
    
    build_cell(row, cell)
    # our lambda gets invoked, which re-passes the arguments it receives to the corresponding implementation
Multimethods doesn't seem to be have been adopted at all in the Ruby community. A quick googling reveals a couple implementations though.

In the Lisp world they are first-class, even though they aren't used all the time. http://clojure.org/multimethods might be worth a read.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: