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
29
votes
1 answer

Determining whether a function has standard evaluation

Is there any way to programmatically tell if a given function in r has standard evaluation, and if not, which component of function evaluation – parsing, matching, scoping, promise formation, promise fulfillment, return, etc. – is…
andrewH
  • 2,281
  • 2
  • 22
  • 32
29
votes
2 answers

Ruby's def and instance_eval vs. class_eval

I'm reading the Metaprogramming section of Programming Ruby 1.9 and I'm having trouble understanding what's going on internally between class_eval/class_exec vs. instance_eval/instance_exec. So first of all, my understanding is that def adds a…
Jo Liss
  • 30,333
  • 19
  • 121
  • 170
29
votes
4 answers

How to generate nested loops at compile time

I have an integer N which I know at compile time. I also have an std::array holding integers describing the shape of an N-dimensional array. I want to generate nested loops, as described bellow, at compile time, using metaprogramming techniques.…
Teodor Nikolov
  • 783
  • 7
  • 14
29
votes
3 answers

Difference between __callee__ and __method__

In Ruby, one can use either __callee__ or __method__ to find the name of the currently executing method. What is the difference between the two?
port5432
  • 5,889
  • 10
  • 60
  • 97
29
votes
4 answers

Explicit use of LambdaMetafactory

I'm trying to explicitly use LambdaMetafactory.metafactory, I can't understand why it works only with the Runnable functional interface. For Example, this code does what it is expected (it prints "hello world"): public class MetafactoryTest { …
andrebask
  • 774
  • 1
  • 7
  • 18
29
votes
4 answers

How do I trap arguments to a target method when using a Proxy object?

I'm trying to use Javascript Proxy objects to trap the arguments that are passed to a 'method' of the target that I'm proxying. Please consider this example: var test = { doSomething: function() { console.log( arguments.length ); …
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
28
votes
6 answers

Ruby metaprogramming online tutorial

I have just started to learn Ruby and got a good take on the basics. I keep hearing that one of the cool things that Ruby does very well is metaprogramming, but none of the tutorials I've read cover this. Searching Google I can only seem to find…
Robin Barnes
  • 13,133
  • 15
  • 44
  • 45
28
votes
1 answer

Dynamic Class Creation in SQLAlchemy

We have a need to create SQLAlchemy classes to access multiple external data sources that will increase in number over time. We use the declarative base for our core ORM models and I know we can manually specify new ORM classes using the…
PlaidFan
  • 797
  • 11
  • 20
28
votes
4 answers

How can I reverse ruby's include function

I'll explain what i'm looking for in code as thats probably the most succinct: module Mixin def method puts "Foo" end end class Whatever include Mixin end w = Whatever.new w.method => "Foo" # some magic here w2 =…
Glenjamin
  • 7,150
  • 6
  • 25
  • 26
28
votes
5 answers

C++ Template Metaprogramming - Is it possible to output the generated code?

I would like to debug some templated code to understand it better. Unfortunately I'm new to template metaprogramming and it IS hard for me to get in. When I try to output the preprocessed source files I get 125 000 lines of code :/ So is there a way…
n00ki3
  • 14,529
  • 18
  • 56
  • 65
27
votes
4 answers

Getting a list of classes that include a module

I have a mixin for which I would like to get a list of all the classes that have included it. In the mixin module, I did the following: module MyModule def self.included(base) @classes ||= [] @classes << base.name end def…
jangosteve
  • 1,562
  • 2
  • 14
  • 26
27
votes
9 answers

Can a C program modify its executable file?

I had a little too much time on my hands and started wondering if I could write a self-modifying program. To that end, I wrote a "Hello World" in C, then used a hex editor to find the location of the "Hello World" string in the compiled executable.…
Joel
  • 1,437
  • 2
  • 18
  • 28
27
votes
7 answers

Metaprogramming - self explanatory code - tutorials, articles, books

I am looking into improving my programming skils (actually I try to do my best to suck less each year, as our Jeff Atwood put it), so I was thinking into reading stuff about metaprogramming and self explanatory code. I am looking for something like…
elena
  • 271
  • 4
  • 4
27
votes
5 answers

How to mutate a boxed struct using IL

Imagine we have a mutable struct (yes, don't start): public struct MutableStruct { public int Foo { get; set; } public override string ToString() { return Foo.ToString(); } } Using reflection, we can take a boxed instance of…
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
26
votes
3 answers

What trait / concept can guarantee memsetting an object is well defined?

Let's say I have defined a zero_initialize() function: template T zero_initialize() { T result; std::memset(&result, 0, sizeof(result)); return result; } // usage: auto data = zero_initialize(); Calling zero_initialize()…
YSC
  • 38,212
  • 9
  • 96
  • 149