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
53
votes
6 answers

Dynamic/runtime method creation (code generation) in Python

I need to generate code for a method at runtime. It's important to be able to run arbitrary code and have a docstring. I came up with a solution combining exec and setattr, here's a dummy example: class Viking(object): def __init__(self): …
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
52
votes
2 answers

Accessing function as attribute in a Python class

I'm in a situation where it would be extremely useful (though not strictly necessary) to access a class' instancemethod as an attribute. (it's for an API that uses getattr to set some return values for a dictionary and I don't want to mess the neat…
Brian Hicks
  • 6,213
  • 8
  • 51
  • 77
52
votes
4 answers

Safe way to extract property names

I'm looking for a way to get an object property name with typechecking that allows to catch possible regressions after refactoring. Here's an example: the component where I have to pass the property names as strings and it will be broken if I'll…
shadeglare
  • 7,006
  • 7
  • 47
  • 59
51
votes
3 answers

Thread Safety: Class Variables in Ruby

Performing writes/reads on class variables in Ruby is not thread safe. Performing writes/reads on instance variables appears to be thread safe. That said, is it thread safe to perform write/reads on instance variables of a class or metaclass…
51
votes
4 answers

Tag dispatch versus static methods on partially specialised classes

Suppose I want to write a generic function void f(), which does one thing if T is a POD type and another thing if T is non-POD (or any other arbitrary predicate). One way to achieve this would be to use a tag-dispatch pattern like the standard…
51
votes
6 answers

method_missing gotchas in Ruby

Are there any things to be careful about when defining the method_missing method in Ruby? I'm wondering whether there are some not-so-obvious interactions from inheritance, exception throwing, performance, or anything else.
readonly
  • 343,444
  • 107
  • 203
  • 205
50
votes
1 answer

What is the purpose of C++20 std::common_reference?

C++20 introduces std::common_reference. What is its purpose? Can someone give an example of using it?
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
50
votes
3 answers

Reducing code duplication while defining a commutative operation

I have a a set of overloads of a commutative binary function named overlap, which accepts two distinct types: class A a; class B b; bool overlap(A, B); bool overlap(B, A); My function overlap returns true if and only if one shape overlaps the other…
smiling_nameless
  • 1,047
  • 1
  • 9
  • 23
49
votes
3 answers

Get locals from calling namespace in Python

I want to retrieve the local variables from Python from a called function. Is there any way to do this? I realize this isn't right for most programming, but I am basically building a debugger. For example: def show_locals(): # put something in…
Peter
  • 127,331
  • 53
  • 180
  • 211
49
votes
1 answer

What does "typename =" mean in the template parameters?

I have seen this expression in page 189 of the book "Effective Modern C++": template::type> explicit Person(T&& n); I am just wondering what does the part "typename ="…
qft
  • 705
  • 6
  • 9
49
votes
1 answer

C++11 is_same type trait for templates

Is it possible to check that type T is an std::array of arbitrary type and size? I can check for a particular array, for instance: is_same>::value But I'd like to check that T is any instantiation of std::array. Something like…
user2052436
  • 4,321
  • 1
  • 25
  • 46
48
votes
1 answer

Ruby: Inherit code that works with class variables

The situation: I have multiple classes that should each hold a variable with a configuration hash; a different hash for each class but the same for all instances of a class. At first, i tried like this class A def self.init config @@config =…
Adam Nonymous
47
votes
7 answers

Getting template metaprogramming compile-time constants at runtime

Background Consider the following: template struct Fibonacci { enum { value = Fibonacci::value + Fibonacci::value }; }; template <> struct Fibonacci<1> { enum { value = 1 …
GManNickG
  • 494,350
  • 52
  • 494
  • 543
47
votes
1 answer

How to call a method dynamically in Elixir, by specifying both module and method name?

I'd like to know what exactly a method name is in elixir: array = [1,2,3] module_name = :lists method_name = :nth # this not working module_name.method_name(1, array) # error, undef function lists.method_name/2 module_name.nth(1,…
halfelf
  • 9,737
  • 13
  • 54
  • 63
46
votes
4 answers

Change the context/binding inside a block in ruby

I have a DSL in Ruby that works like so: desc 'list all todos' command :list do |c| c.desc 'show todos in long form' c.switch :l c.action do |global,option,args| # some code that's not relevant to this question end end desc 'make a new…
davetron5000
  • 24,123
  • 11
  • 70
  • 98