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

This is something that has bothered me about my Ruby usage. A lot of the time, I feel like I'm writing "dynamic" code that really just needs to be pre-processed.

  class Cool
    [:ice, :soda, :snow].each do |item|
      define_method item do
        #some code
      end
    end
  end
There isn't a real reason why that couldn't be pre-processed. I don't know a ton of Java, but the idea of compile-time code generation is appealing. Does anyone know more about this?


Solving this problem in general is a major area of research. Look up "Partial Evaluation."


Yes! I second this. Partial evaluation is especially compelling stuff for improving efficiency. I'm glad other people on HN are into it.


That code is only run once per application lifetime. Without risking DRY, I don't know what you mean by 'pre-processed'. Do you care if it is run at the parse time instead of the Class Object definition time?


Pre-processed here likely means what you think it means. DRY is not really a concept meant to be applied to method definitions (IMO), but rather method bodies-- of course there are many ways to abuse the interpretation of DRY as well. As far as whether you should care about compile vs load time-- in Ruby you don't need to, but if the language had static typing, it would make a difference.


I think the reason is merely that compile-time metaprogramming hasn't been adopted in any mainstream programming language (yet).

Boo can do cool things like that though:

http://bamboo.github.com/2010/07/11/boo-meta-programming-fac...




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

Search: