metaprogramming

Is it legal to extend the Class class?

I've been writing a system that at runtime generates some templates, and then generates some objects based on those templates. I had the idea that the templates could be extensions of the Class class, but that resulted in some magnificent errors: VerifyError: Error #1107: The ABC data is corrupt, attempt to read out of bounds. What I'...

How to retrieve caller context object in Ruby ?

Hi, hereafter is my piece of code that I want to simplify in order to avoid passing an extra argument on each call. In fact, my usecase is that M is a user library without the definition of context argument on each method. check is a method that is not defined by the user. # User code module M def do_something(context) puts "Calle...

Java Code Generation (Metaprogramming, Reflection, wtv)

Does anyone knows a tool for Java (something like codedom for C#) that provides a way to generate Java code to a .java file? EDIT: I'm building a plataform that its main objective his to automate an operation. Giving some input, i want to generate code for an external tool. So it isn't generation on runtime. I want to generate and outpu...

C++: How to require that one template type is derived from the other

In a comparison operator: template<class R1, class R2> bool operator==(Manager<R1> m1, Manager<R2> m2) { return m1.internal_field == m2.internal_field; } Is there any way I could enforce that R1 and R2 must have a supertype or subtype relation? That is, I'd like to allow either R1 to be derived from R2, or R2 to be derived from R...

Software tools for allowing end users to reprogram interfaces

What would be the examples of commercial software products (specially web-services) which allow the end user to reprogram their user-interface? I mean end users who do not know programming and they are allowed to add more functionality. One way of doing it is allowing XML gadgets like iGoogle does. What are the other approaches and also ...

C++ meta Programming to create a new typelist with remove_const applied to each element

Hi Could anyone give me a sample program to "Create an ApplyRemoveConst template that constructs a new typelist with remove_const applied to each element" For example: typedef TYPELIST_3(A, const B, B) TL; typedef ApplyRemoveConst<TL>::Result TL2; // TL2 is the same as TYPELIST_3(A, B, B) //Typelist Definition: template<class T, cla...

Just introducing myself to TMPing, and came across a quirk

I was just trying to learn the syntax of the beginner things, and how it worked when I was making this short bit of code in VS2008. The code below works in adding numbers 1 to 499, but if I add 1 to 500, the compiler bugs out giving me: fatal error C1001: An internal error has occurred in the compiler. And I was just wondering why that...

How do you compare two unknown numbers to see if they're equal in a special case template?

Here, is my code. Just trying to wrap my head around some of the basic things you can do with TMP. I'm trying to supply two numbers with which the compiler will add up that range of numbers. I'm just not sure how to write the syntax for the "constraint" template. template < int b, int e > struct add { enum { sum = add< b + 1, e >::s...

Compile time Meta-programming, with string literals.

I'm writing some code which could really do with some simple compile time metaprogramming. It is common practise to use empty-struct tags as compile time symbols. I need to decorate the tags with some run-time config elements. static variables seem the only way to go (to enable meta-programming), however static variables require global d...

Re-Include Module

Hello, I need some like this: module One def test; puts 'Test One'; end end module Two def test; puts 'Test Two'; end end class Foo include One include Two include One end In this case I need as a result 'Test One' but obviously it returns 'Test Two'. I need a clean simple way for re-include my module. Any suggestion? ...

Function-Local Static Const variable Initialization semantics.

The questions are in bold, for those that cannot be bothered reading a question in depth. This is a followup to this question. It is to do with the initialization semantics of static variables in functions. Static variables should be initialized once, and their internal state might be altered later - as I (currently) do in the linked qu...

How do I assert that a model has a readable/writable attribute?

In one of my model's I'm using some meta programming to dynamically define some methods. I'd like to test this; thus, I need a compact way to assert my model has a readable/writable attribute with a certain name. Ideas? I'm using shoulda for unit testing if it makes a difference. ...

What is Ruby's analog to Python Metaclasses?

Python has the idea of metaclasses that, if I understand correctly, allow you to modify an object of a class at the moment of construction. You are not modifying the class, but instead the object that is to be created then initialized. Python (at least as of 3.0 I believe) also has the idea of class decorators. Again if I understand cor...

Ruby: Dynamically calling available methods raising undefined method (metaprogramming)

I have an Activerecord object called Foo: Foo.attribute_names.each do |attribute| puts Foo.find(:all)[0].method(attribute.to_sym).call end Here I'm calling all attributes on this model (ie, querying for each column value). However, sometimes, I'll get an undefined method error. How can ActiveRecord::Base#attribute_names return an ...

C# - Recursive / Reflection Property Values

What is the best way to go about this in C#? string propPath = "ShippingInfo.Address.Street"; I'll have a property path like the one above read from a mapping file. I need to be able to ask the Order object what the value of the code below will be. this.ShippingInfo.Address.Street Balancing performance with elegance. All object gr...

Reduce boilerplate of adding functions to a function queue

I'm working with animation in JavaScript, and I have a bunch of functions you can call to add things to the animation queue. Basically, all of these functions look like this: function foo(arg1, arg2) { _eventQueue.push(function() { // actual logic } } I'm wondering now if it would be possible to cut down on this boile...

How do I use Ruby metaprogramming to refactor this common code?

I inherited a project with a lot of badly-written Rake tasks that I need to clean up a bit. Because the Rakefiles are enormous and often prone to bizarre nonsensical dependencies, I'm simplifying and isolating things a bit by refactoring everything to classes. Specifically, that pattern is the following: namespace :foobar do desc "Fr...

What libraries use design patterns implemented with compile-time metaprogramming techniques?

Does anybody know of any libraries that use design patterns that are implemented using compile-time techniques e.g. template metaprogramming? I know that Loki implements a few but I need to find other libraries. ...

How can I iterate through all of the Models in my rails app?

I would like to be able to iterate over and inspect all the models in my rails app. In pseudo-code it would look something like: rails_env.models.each do |model| associations = model.reflect_on_all_associations(:has_many) ... do some stuff end My question is how do I inspect my rails app to get a collection of the models (ra...

Programatically importing a file and creating an instance of a class in Python

I have a (python) list of strings which refer to python source files and subsequently classes within those files which I want to import and then create an instance of the classes within the files (everything follows a strict naming convention, making this theoretically possible), in Ruby I would do something like: require "lib/sources/#...