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
491
votes
20 answers

Python dictionary from an object's fields

Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this: >>> class Foo: ... bar = 'hello' ... baz = 'world' ... >>> f = Foo() >>> props(f) { 'bar' : 'hello', 'baz' :…
Julio César
  • 12,790
  • 10
  • 38
  • 45
225
votes
5 answers

Get the name of the currently executing method

$0 is the variable for the top level Ruby program, but is there one for the current method?
salt.racer
  • 21,903
  • 14
  • 44
  • 51
194
votes
8 answers

What exactly is metaprogramming?

I was reading an article on TheServerSide on ployglot programming on the Java platform. Some comments in the article refer to metaprogramming as the ability to generate code (perhaps on the fly). Is metaprogramming the ability to generate code on…
Parag
  • 12,093
  • 16
  • 57
  • 75
181
votes
4 answers

Calling a Method From a String With the Method's Name in Ruby

How can I do what they are talking about here, but in Ruby? How would you do the function on an object? and how would you do a global function (see jetxee's answer on the post mentioned)? EXAMPLE CODE: event_name = "load" def load() puts "load()…
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
174
votes
5 answers

Is it possible to implement dynamic getters/setters in JavaScript?

I am aware of how to create getters and setters for properties whose names one already knows, by doing something like this: // A trivial example: function MyObject(val){ this.count = 0; this.value = val; } MyObject.prototype = { get…
daiscog
  • 11,441
  • 6
  • 50
  • 62
171
votes
10 answers

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?
rlbond
  • 65,341
  • 56
  • 178
  • 228
165
votes
4 answers

How do you pass arguments to define_method?

I would like to pass an argument(s) to a method being defined using define_method, how would I do that?
Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62
163
votes
20 answers

Conveniently Declaring Compile-Time Strings in C++

Being able to create and manipulate strings during compile-time in C++ has several useful applications. Although it is possible to create compile-time strings in C++, the process is very cumbersome, as the string needs to be declared as a variadic…
void-pointer
  • 14,247
  • 11
  • 43
  • 61
152
votes
5 answers

Can a line of Python code know its indentation nesting level?

From something like this: print(get_indentation_level()) print(get_indentation_level()) print(get_indentation_level()) I would like to get something like this: 1 2 3 Can the code read itself in this way? All I want is the output from…
147
votes
5 answers

Is it possible to figure out the parameter type and return type of a lambda?

Given a lambda, is it possible to figure out it's parameter type and return type? If yes, how? Basically, I want lambda_traits which can be used in following ways: auto lambda = [](int i) { return long(i*10);…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
144
votes
8 answers

What does send() do in Ruby?

Can someone please tell me what the following snippet obj.send("#{method_name}") is and does?
Christian Bankester
  • 2,044
  • 2
  • 15
  • 18
131
votes
3 answers

Objective-C class -> string like: [NSArray className] -> @"NSArray"

I am trying to get a string name of a class from the class object itself. // For instance [NSArray className]; // @"NSArray" I have found object_getClassName(id obj) but that requires an instance be passed to it, and in my case that is needless…
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
124
votes
1 answer

How Pony (ORM) does its tricks?

Pony ORM does the nice trick of converting a generator expression into SQL. Example: >>> select(p for p in Person if p.name.startswith('Paul')) .order_by(Person.name)[:2] SELECT "p"."id", "p"."name", "p"."age" FROM "Person" "p" WHERE…
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
123
votes
13 answers

How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?

In a recent interview, I was asked a really strange question. The interviewer asked me how can I compute 1+2+3+...+1000 just using compiler features. This means that I am not allowed to write a program and execute it, but I should just write a…
TonySalimi
  • 8,257
  • 4
  • 33
  • 62
121
votes
8 answers

Best introduction to C++ template metaprogramming?

Static metaprogramming (aka "template metaprogramming") is a great C++ technique that allows the execution of programs at compile-time. A light bulb went off in my head as soon as I read this canonical metaprogramming example: #include…
jwfearn
  • 28,781
  • 28
  • 95
  • 122
1
2 3
99 100