metaprogramming

Template metaprogram converting type to unique number

I just started playing with metaprogramming and I am working on different tasks just to explore the domain. One of these was to generate a unique integer and map it to type, like below: int myInt = TypeInt<AClass>::value; I want to know if this is at all possible, and in that case how. Because although I have learned much about explor...

Why can't I use attr_accessor inside initialize?

I'm trying to do an instance_eval followed by a attr_accessor inside initialize, and I keep getting this: `initialize': undefined method 'attr_accessor'. Why isn't this working? The code looks kind of like this: class MyClass def initialize(*args) instance_eval "attr_accessor :#{sym}" end end ...

How can I get this snippet to work?

I'd like to port a little piece of code from Ruby to Groovy, and I'm stuck at this: def given(array,closure) { closure.delegate = array closure() } given([1,2,3,4]) { findAll { it > 4} } Right now it dies with this message: Exception thrown: Cannot compare ConsoleScript0$_run_closure1 with value 'ConsoleScript0$_run_closu...

C++ implicit template instantiation

I currently have a class hierarchy like MatrixBase -> DenseMatrix -> (other types of matrices) -> MatrixView -> TransposeView -> DiagonalView -> (other specialized views of matrices) MatrixBase is an abstract class which forces implementers to define operator()(in...

Can I extract C++ template arguments out of a template class?

Basically, given a template class like this: template< class Value > class Holder { }; I would like to be able to discover the type Value for a given Holder class. I thought that I would be able to make a simple metafunction that takes a template template argument, like this: template< template< class Value > class Holder > class Ge...

How to find the current stack?

Hi, in Pharo, how can I find the currently evaluating stack? ...

Populating values in module namespace

I have a python module. I want to populate some values to it at runtime, how do I do it. Eg. I have a list, ['A', 'B', 'C'] I am creating there classes with these names, and want them to available as if I created them normally for el in ['A', 'B', 'C']: type(el, (object,), {}) ...

Using Polymorphic Code for Legitimate Purposes?

Hi, I recently came across the term Polymorphic Code, and was wondering if anyone could suggest a legitimate (i.e. in legal and business appropriate software) reason to use it in a computer program? Links to real world examples would be appreciated! Before someone answers, telling us all about the benefits of polymorphism in object or...

C++ Preprocessor metaprogramming: obtaining an unique value?

Hi, I'm exploiting the behavior of the constructors of C++ global variables to run code at startup in a simple manner. It's a very easy concept but a little difficult to explain so let me just paste the code: struct _LuaVariableRegistration { template<class T> _LuaVariableRegistration(const char* lua_name, const T& c_name) { ...

How do I add a method to a ruby gem without editing the gem source?

I am using the acts_as_taggable_on gem and would like to add a method to one of the gem source files (tag.rb), but I do not want to change the gem source in any way. I have tried creating my own tag.rb file to in the /app/models directory or in the /lib directory, and then adding the desired method to that file expecting that ruby will ...

Is implementing SOAP clients in Perl using meta-programming sensible?

I'm currently dealing with a code base which contains several dozens of classes generated with SOAP::WSDL. However, having worked with Moose I now think that generating those classes at runtime at meta level (i.e. not to files on disk but directly to objects) might be a better idea (completely excluding performance reasons at this point)...

Python on Rails?

Would it be possible to translate the Ruby on Rails code base to Python? I think many people like Python more than Ruby, but find Ruby on Rails features better (as a whole) than the ones in Python web frameworks. So that, would it be possible? Or does Ruby on Rails utilize language-specific features that would be difficult to translate...

Overload a method with a function at runtime

OK, I'll admit upfront this is a mega kludge and that I could definately implement this better. It's only morbid curiosity that's driving me to find out how I could do this. class SomeClass(object): def __init__(self): def __(self, arg): self.doStuff(arg) self.overLoaded = __ def doStuff(self, string)...

python modify __metaclass__ for whole program

EDIT: Note that this is a REALLY BAD idea to do in production code. This was just an interesting thing for me. Don't do this at home! Is it possible to modify __metaclass__ variable for whole program (interpreter) in Python? This simple example is working: class ChattyType(type): def __init__(cls, name, bases, dct): print...

How do you set the class of an object to something else?

Hi, I've seen this recently and now I can't find it … How do you set the class of an object to something else? --Update: Well, in Pharo! Like: d:=Object new. d setClass: Dictionary. Only that it isn't actually setClass. How can you modify the class pointer of an object? ...

Get the templated type as a string

After reading C++ compile-time string hashing with Boost.MPL, and considering a problem I have, the following came to my mind. I have the base class: template<class Command> class Base { typedef Command CommandType; } It is supposed to be a utility base class for the Commands classes, so they don't need to typedef and declare some ...

Overriding the "for" keyword in Ruby. Is it possible?

I search around and tried overriding the "for" keyword but I found nothing. I am trying something like that: def for("maybe_arguments_go_here") print "Hello from function for!" end for i in 1..3 print "Hello from for" end ...

What does first argument to `type` do?

Some code. In [1]: A = type('B', (), {}) In [2]: a = A() In [3]: b = B() --------------------------------------------------------------------------- NameError Traceback (most recent call last) /home/shabda/<ipython console> in <module>() NameError: name 'B' is not defined What does first argument to...

Preprocessor metaprogramming library for plain C

Hi, does anybody know a library similar to boost::preprocessor (maybe not so advanced) that could be easily used/incorporated in plain C projects? Of course, the most (all ?) of boost::preprocessor is usable when writing in C but I would prefer a small library with only basic capabilities that doesn't depend on the monster like boost. ...

Are there machine (and human) readable specification languages for algorithm collections and meta-libraries?

Is there high-level language out there for describing algorithms, that's geared towards specification, rather than implementation? The idea would be to have a machine-readable archive of standard algorithms, with machine-readable annotations on trade-offs, and variants. I'm thinking of something like CycL / OpenCyC, but for algorithms ...