Questions tagged [metaprogramming]

Metaprogramming is the capability to reprogram ones programming environment, as with macros or metaclasses.

Metaprogramming - the capability to reprogram ones programming environment - can save time and code by allowing one to create new abstractions, or new and more concise syntax for repetitive tasks.

Notable metaprogramming facilities include:

  • Common lisp macros
  • Scheme hygienic macros
  • Common lisp metaobject protocol
  • Python metaclass facility
  • Smalltalk's entirely transparent and reflective execution environment

For more info see http://en.wikipedia.org/wiki/Metaprogramming.

4722 questions
37
votes
6 answers

Pickling dynamically generated classes?

I'm using type() to dynamically generate classes that will ultimately be pickled. The problem is that the un-pickling process needs the definition of the class in order to re-construct the object that has been pickled. This is where I'm stuck. I…
reckoner
  • 2,861
  • 3
  • 33
  • 43
36
votes
2 answers

Add/Remove data members with template parameters?

Consider the following code : template class MyClass { public: void myFunction(); template::type> void addedFunction(); protected: double myVariable; …
Vincent
  • 57,703
  • 61
  • 205
  • 388
36
votes
4 answers

Valid characters in a python class name

I'm dynamically creating python classes, and I know not all characters are valid in this context. Is there a method somewhere in the class library that I can use to sanitize a random text string, so that I can use it as a class name? Either that or…
Filipe Correia
  • 5,415
  • 6
  • 32
  • 47
35
votes
2 answers

Automatically setting an enum member's value to its name

I've been messing around with python's enum library and have come across a conundrum. In the docs, they show an example of an auto-numbering enum, wherein something is defined: class Color(AutoNumber): red = () green = () ... I want to…
Keelan Armstrong
  • 351
  • 1
  • 3
  • 3
34
votes
5 answers

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…
Federico Builes
  • 4,939
  • 4
  • 34
  • 48
34
votes
4 answers

Ruby: How to chain multiple method calls together with "send"

There has to be a built in way of doing this, right? class Object def send_chain(arr) o=self arr.each{|a| o=o.send(a) } return o end end
Zando
  • 5,473
  • 8
  • 30
  • 37
34
votes
8 answers

Ruby - print the variable name and then its value

What is the best way to write a function (or something DSLish) that will allow me to write this code in Ruby. How would I construct the function write_pair? username = "tyndall" write_pair username # where write_pair username outputs username:…
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
33
votes
3 answers

How do I get a list of files that have been `required` in Ruby?

This is purely an experiment, but I'm wondering if it's possible to get a list of the require'd gems at runtime via some kind of metaprogramming. For example, say I have: require 'rubygems' require 'sinatra' require 'nokogiri' # don't know what to…
Jihan
  • 333
  • 1
  • 3
  • 4
33
votes
10 answers

Syntactic sugar in C/C++

I have been looking into Ruby and find its keywords "until" and "unless" very interesting. So I thought what was a good way to add similar keywords into C/C++. This is what I came up with: #define until(x) while(!(x)) #define unless(x) …
BiGYaN
  • 6,974
  • 5
  • 30
  • 43
33
votes
2 answers

easing c++ to objective-c/cocoa bridging via metaprogramming?

In a pure C++ world we can generate interfacing or glue code between different components or interfaces at compile time, using a combination of template-based compile-time and runtime-techniques (to e.g. mostly automatically marshall to/from calls…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
33
votes
3 answers

How do you evaluate a string as a clojure expression?

How would I get something similar to the following?: (evaluate-text "(+ 1 2)") ; resolves to 3
Nick Orton
  • 3,563
  • 2
  • 21
  • 28
33
votes
4 answers

How to know if a type is a specialization of std::vector?

I've been on this problem all morning with no result whatsoever. Basically, I need a simple metaprogramming thing that allows me to branch to different specializations if the parameter passed is a kind of std::vector or not. Some kind of is_base_of…
Michael
  • 1,357
  • 3
  • 15
  • 24
33
votes
4 answers

Design principles behind std::ratio<>

I was looking at the class std::ratio<> from the C++11 standard that allows to make compile-time rational arithmetic. I found the template design and the operations implemented with classes overly complex and did not find any reason why they could…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
32
votes
5 answers

Dynamically set local variables in Ruby

I'm interested in dynamically setting local variables in Ruby. Not creating methods, constants, or instance variables. So something like: args[:a] = 1 args.each_pair do |k,v| Object.make_instance_var k,v end puts a > 1 I want locally variables…
Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
32
votes
3 answers

How to list local-variables in Ruby?

def method a = 3 b = 4 some_method_that_gives # [a, b] end
Cheng
  • 4,816
  • 4
  • 41
  • 44