metaprogramming

Reflection: for frameworks only?

Somebody that I work with and respect once remarked to me that there shouldn't be any need for the use of reflection in application code and that it should only be used in frameworks. He was speaking from a J2EE background and my professional experience of that platform does generally bear that out; although I have written reflective app...

How do you call attr_accessible dynamically in Rails?

I have a rather unique class that allows its child classes to declare virtual fields. The child can declare virtual fields stored as XML by calling a method of the parent class like this: class Child1 < Parent create_xml_field ["readings", "usage"] end I have managed to get it working via a nasty work around. The create_xml_field me...

Find classes available in a Module

I really must be missing something here. Say I have a module MyModule. I dynamically load classes into it. How can I get a list of the classes defined within its namespace? Example: def load_plugins Dir.glob(File.dirname(__FILE__) + '/plugins/*.rb') do |f| MyModule.class_eval File.read(f) end # now how can I find the new cla...

What exactly is Reflection and when is it a good approach?

What exactly is Reflection? I read the Wikipedia article on this subject and I understand that it is a kind of meta-programming, where the program can modify itself at run-time, but what does this mean? In what kind of situations is this a good approach and when is it the best to use it? ...

How to uniquely identify a user defined type in D?

I need to generate something that can be used as a unique handle for a user defined type (struct or class) in the D programming language. Preferably this would be a compile time computable value. I want the handle to relate to the name of the type as well as change if the internal structure (data layout) of the type changes but remain th...

Getting template metaprogramming compile-time constants at runtime

Background Consider the following: template <unsigned N> struct Fibonacci { enum { value = Fibonacci<N-1>::value + Fibonacci<N-2>::value }; }; template <> struct Fibonacci<1> { enum { value = 1 }; }; template <> struct Fibonacci<0> { enum { value = 0 }; }; This is a common example ...

C++ template specialization problem

I need a C++ template that, given a type and an object of that type, it can make a decision based on whether the type is an integer or not, while being able to access the actual objects. I tried this template <typename T, T &N> struct C { enum { Value = 0 }; }; template <int &N> struct C<int, N> { enum { Value = N }; }; but i...

How to change behaviour of the methed in groovy using that method in metaclass

I would like to "spoil" plus method in Groovy in the following way: Integer.metaClass.plus {Integer n -> delegate + n + 1} assert 2+2 == 5 I am getting StackOverflowException (which is not surprising). Is there any way to use "original" plus method inside metaclass' closure? ...

How do I call +class methods in Objective C without referencing the class?

I have a series of "policy" objects which I thought would be convenient to implement as class methods on a set of policy classes. I have specified a protocol for this, and created classes to conform to (just one shown below) @protocol Counter +(NSInteger) countFor: (Model *)model; @end @interface CurrentListCounter : NSObject <Count...

PowerShell MetaProgramming - Generating Advanced Functions

I am looking at dynamically building a bunch of Advanced Functions. I have been using New-PSScript for this but it doesn't allow for all the flexibility I am looking for. I was reading the man page for about functions advanced parameters and saw something about Dynamic Parameters at the end of the help article which gives the following...

Extend for one block call only

I have a class that contains some private attributes. What I would like to do is to dynamically add some setters for these only for the execution of a specific block. Example of what I would like to be able to: class Content attr_reader :a, :b def initialize @a = 1 @b = "plop" end def set(&block) extend(Setter) ...

When/Why ( if ever ) should i think about doing Generic Programming/Meta Programming

Hi there IMHO to me OOPS, design patterns make sense and i have been able to apply them practically. But when it comes to "generic programming /meta programming" of the Modern C++ kind, i am left confused. -- Is it a new programming/design paradigm ? -- Is it just limited to "library development"? If not, What design/coding situatio...

[C++] What is metaprogramming?

With reference to this question, could anybody please explain and post example code of metaprogramming? I googled the term up, but I found no examples to convince me that it can be of any practical use. On the same note, is Qt's Meta Object System a form of metaprogramming? jrh ...

C++ SFINAE examples?

I want to get into more template meta-programming. I know that SFINAE stands for "substitution failure is not an error." But can someone show me a good use for SFINAE? ...

Dynamic Language Features and Meta-Programming Used in Django

Any good summary articles of the dynamic language and meta-programming features of Python that get utilized by Django? Or can we build that out here? Setting this up as a wiki-style entry. ...

Ruby: print the code of an arbitrary method (and exec in context)

I would like to do something like the following: class String def fancy_thing appendix # Just a trivial example to ensure self and params work. # Pretend this is something complex. self.reverse + appendix end end # print_method on instance or class should spit out a string # containing the actual code for that method ft_c...

How to find out the arity of a method in Python

I'd like to find out the arity of a method in Python (the number of parameters that it receives). Right now I'm doing this: def arity(obj, method): return getattr(obj.__class__, method).func_code.co_argcount - 1 # remove self class Foo: def bar(self, bla): pass arity(Foo(), "bar") # => 1 I'd like to be able to achieve this...

Why doesn't Ruby automatically execute to_s?

I have an author class: class Author < ActiveRecord::Base def to_s name end end Defining to_s allows me to do puts Author.first, but not puts Author.first.rjust(10): NoMethodError: undefined method `rjust' for #<Author:0x21eb5d4> Wouldn't it be better if Ruby automatically tried to_s before the string method in cases like t...

Can I use the Curiously Recurring Template Pattern here (C++)?

I have a C++ application that can be simplified to something like this: class AbstractWidget { public: virtual ~AbstractWidget() {} virtual void foo() {} virtual void bar() {} // (other virtual methods) }; class WidgetCollection { private: vector<AbstractWidget*> widgets; public: void addWidget(AbstractWidget* widget) {...

Meta Programming System Tutorials

I have recently downloaded Jetbrains' Meta Programming System and am impressed. However, I would like some materials/tutorials that help put it to practical use. Does anyone know where i might find such material? ...