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
21
votes
2 answers

Preventing non-const lvalues from resolving to rvalue reference instead of const lvalue reference

I'm having trouble overloading a function to take a value either by const reference or, if it is an rvalue, an rvalue reference. The problem is that my non-const lvalues are binding to the rvalue version of the function. I'm doing this in…
Ayjay
  • 3,413
  • 15
  • 20
21
votes
5 answers

Meta-programming: write in one language X, cross-compile in multiple languages like C#, PHP, Java, C

In all projects I've done through the years I never came across a requirement like this, though it seems so easy on paper: write a plugin for many well-known CMS's. Obviously, each plugin-system (or extension system) is different and this requires…
Abel
  • 56,041
  • 24
  • 146
  • 247
21
votes
6 answers

Generic programming vs. Metaprogramming

What exactly is the difference? It seems like the terms can be used somewhat interchangeably, but reading the wikipedia entry for Objective-c, I came across: In addition to C’s style of procedural programming, C++ directly supports certain…
21
votes
5 answers

Python, import string of Python code as module

In python you can do something like this to import a module using a string filename, and assign its namespace a variable on the local namespace. x = __import__(str) I'm wondering if there is a related function that will take take a string of Python…
Mike
  • 58,961
  • 76
  • 175
  • 221
21
votes
4 answers

Examples of what D’s templates can be used for

I hear that the D language has powerful metaprogramming features for executing functions at compile time. That sounds very exciting, but I find it difficult to think of practical examples of things that are hard to accomplish without them. Can…
Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
21
votes
1 answer

How do I use class_eval?

I don't understand class_eval. class Module def attr_ (*syms) syms.each do |sym| class_eval %{def #{sym}= (val) @#{sym} = val end} end end end What does the % mean? What does class_eval do? And where is (val) coming…
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383
21
votes
1 answer

Generating Clojure code with macro containing type hints

I'm trying to generate some Clojure code with type hints, however the type hints seem to disappear whenever I build some code (they also don't function when the code is compiled) e.g. `(let [^BufferedImage b (create-buffered-image)] (.getRGB b 0…
mikera
  • 105,238
  • 25
  • 256
  • 415
20
votes
2 answers

Conversion operator template specialization

Here's a largely academic exercise in understanding conversion operators, templates and template specializations. The conversion operator template in the following code works for int, float, and double, but fails when used with std::string... sort…
Nathan
  • 4,777
  • 1
  • 28
  • 35
20
votes
3 answers

Is there any Template Haskell tutorial for someone who doesn't know Lisp?

I wanted to learn Template Haskell but all tutorials I find either assume that you learned lisp and know what lisp macros are, or that you know some cs theory jargon - things as splices, quasiquotations, etc... - or some theoretical results about…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
20
votes
3 answers

Dynamic constant definition in Rails

I'm defining a constant in an initializer in Rails using the following syntax: MyModule.const_set('MYCONSTANT','foobar') It works, if I start a console and write MyModule::MYCONSTANT I receive foobar as expected. The problem is, when I try to…
VP.
  • 5,122
  • 6
  • 46
  • 71
20
votes
8 answers

JavaScript automatic getter/setters (John Resig book)

I'm reading "Pro JavaScript Techniques" by John Resig, and I'm confused with an example. This is the code: // Create a new user object that accepts an object of properties function User( properties ) { // Iterate through the properties of the…
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
20
votes
10 answers

Tricks for generating SQL statements in Excel

Do you have any tricks for generating SQL statements, mainly INSERTs, in Excel for various data import scenarios? I'm really getting tired of writing formulas with like ="INSERT INTO Table (ID, Name) VALUES (" & C2 & ", '" & D2 & "')"
David Vidmar
  • 3,042
  • 6
  • 29
  • 35
20
votes
5 answers

In Python, how can you get the name of a member function's class?

I have a function that takes another function as a parameter. If the function is a member of a class, I need to find the name of that class. E.g. def analyser(testFunc): print testFunc.__name__, 'belongs to the class, ... I thought …
Charles Anderson
  • 19,321
  • 13
  • 57
  • 73
20
votes
4 answers

Obtain container type from (its) iterator type in C++ (STL)

It is easy given a container to get the associated iterators, example: std::vector::iterator i; //An iterator to a std::vector I was wondering if it is possible, given an iterator type, to deduce the type of the "corresponding…
StephQ
  • 2,032
  • 3
  • 19
  • 27
20
votes
4 answers

Is it possible to replace groovy method for existing object?

The following code tried to replace an existing method in a Groovy class: class A { void abc() { println "original" } } x= new A() x.abc() A.metaClass.abc={-> println "new" } x.abc() A.metaClass.methods.findAll{it.name=="abc"}.each {…
Jean Barmash
  • 4,788
  • 1
  • 32
  • 40