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

Why do I get an error trying to call a template member function with an explicit type parameter?

I don't get it, it seems to me that the call to f is completely unambiguous, but it fails to compile with expected primary-expression before ‘int’. If I comment out the line with the call to f, it compiles fine. template struct A { …
Casey Rodarmor
  • 14,878
  • 5
  • 30
  • 33
75
votes
14 answers

Programmatically create static arrays at compile time in C++

One can define a static array at compile time as follows: const std::size_t size = 5; unsigned int list[size] = { 1, 2, 3, 4, 5 }; Question 1 - Is it possible by using various kinds of metaprogramming techniques to assign these values…
Hippicoder
  • 1,565
  • 3
  • 17
  • 21
72
votes
5 answers

Dynamic method calling in Ruby

As far as I am aware there are three ways to dynamically call a method in Ruby: Method 1: s = SomeObject.new method = s.method(:dynamic_method) method.call Method 2: s = SomeObject.new s.send(:dynamic_method) Method 3: s = SomeObject.new eval…
Abraham P
  • 15,029
  • 13
  • 58
  • 126
68
votes
3 answers

java custom annotation: make an attribute optional

I defined my own custom annotation @Target(value={ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface MyCustomAnnotation { Class myType(); } how, if at all, can I make the attribute optional
flybywire
  • 261,858
  • 191
  • 397
  • 503
67
votes
7 answers

Can you make custom operators in C++?

Is it possible to make a custom operator so you can do things like this? if ("Hello, world!" contains "Hello") ... Note: this is a separate question from "Is it a good idea to..." ;)
Cogwheel
  • 22,781
  • 4
  • 49
  • 67
66
votes
3 answers

Ruby: define_method vs. def

As a programming exercise, I've written a Ruby snippet that creates a class, instantiates two objects from that class, monkeypatches one object, and relies on method_missing to monkeypatch the other one. Here's the deal. This works as…
gauth
  • 908
  • 1
  • 7
  • 11
65
votes
10 answers

Metaprogramming in C++ and in D

The template mechanism in C++ only accidentally became useful for template metaprogramming. On the other hand, D's was designed specifically to facilitate this. And apparently it's even easier to understand (or so I've heard). I've no experience…
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
64
votes
9 answers

Python decorator makes function forget that it belongs to a class

I am trying to write a decorator to do logging: def logger(myFunc): def new(*args, **keyargs): print 'Entering %s.%s' % (myFunc.im_class.__name__, myFunc.__name__) return myFunc(*args, **keyargs) return new class…
Charles Anderson
  • 19,321
  • 13
  • 57
  • 73
63
votes
7 answers

Python dynamic function creation with custom names

Apologies if this question has already been raised and answered. What I need to do is very simple in concept, but unfortunately I have not been able to find an answer for it online. I need to create dynamic functions in Python (Python2.7) with…
61
votes
4 answers

Template Metaprogramming - Difference Between Using Enum Hack and Static Const

I'm wondering what the difference is between using a static const and an enum hack when using template metaprogramming techniques. EX: (Fibonacci via TMP) template< int n > struct TMPFib { static const int val = TMPFib< n-1 >::val + TMPFib<…
Anonymous
  • 4,167
  • 11
  • 47
  • 52
59
votes
1 answer

Is stateful metaprogramming ill-formed (yet)?

One of my most beloved/evil inventions I've had the fortune to come across is the constexpr counter, aka stateful metaprogramming. As mentioned in the post, it seems to be legal under C++14, and I'm wondering has anything changed with C++17? The…
Passer By
  • 19,325
  • 6
  • 49
  • 96
57
votes
2 answers

`respond_to?` vs. `respond_to_missing?`

What is the point of defining respond_to_missing? as opposed to defining respond_to?? What goes wrong if you redefine respond_to? for some class?
sawa
  • 165,429
  • 45
  • 277
  • 381
55
votes
8 answers

Does Javascript have something like Ruby's method_missing feature?

In Ruby I think you can call a method that hasn't been defined and yet capture the name of the method called and do processing of this method at runtime. Can Javascript do the same kind of thing ?
user310291
  • 36,946
  • 82
  • 271
  • 487
55
votes
6 answers

Remove/undef a class method

You can dynamically define a class method for a class like so: class Foo end bar = %q{def bar() "bar!" end} Foo.instance_eval(bar) But how do you do the opposite: remove/undefine a class method? I suspect Module's remove_method and undef_method…
Brian Ploetz
  • 745
  • 1
  • 6
  • 10
54
votes
5 answers

Undefine variable in Ruby

Let's say I'm using irb, and type a = 5. How do I remove the definition of a so that typing a returns a NameError? Some context: later I want to do this: context = Proc.new{}.binding context.eval 'a = 5' context.eval 'undef a' # though this doesn't…
Peter
  • 127,331
  • 53
  • 180
  • 211