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
23
votes
4 answers

Dynamically update ModelForm's Meta class

I am hoping to dynamically update a ModelForm's inline Meta class from my view. Although this code seems to update the exclude list in the Meta class, the output from as_p(), as_ul(), etc does not reflect the updated Meta exclude. I assume then that…
ashchristopher
  • 25,143
  • 18
  • 48
  • 49
23
votes
3 answers

What is "for" in Ruby

In Ruby: for i in A do # some code end is the same as: A.each do |i| # some code end for is not a kernel method: What exactly is "for" in ruby Is there a way to use other keywords to do similar things? Something like: total = sum i in…
David Nehme
  • 21,379
  • 8
  • 78
  • 117
23
votes
2 answers

Is it possible to determine if a type is a scoped enumeration type?

Is there a type trait, or is it possible to write a type trait is_scoped_enum such that: if T is a scoped enumeration, is_scoped_enum::value is true and if T is any other type, is_scoped_enum::value is false
James McNellis
  • 348,265
  • 75
  • 913
  • 977
22
votes
3 answers

Ruby - Using class_eval to define methods

I'm doing the SaaS Stanford class, trying to do Part 5 of this assignment I'm having a really hard time grasping this concept, this is what I've attempted to do: class Class def attr_accessor_with_history(attr_name) attr_name = attr_name.to_s …
8vius
  • 5,786
  • 14
  • 74
  • 136
22
votes
5 answers

Statically Typed Metaprogramming?

I've been thinking about what I would miss in porting some Python code to a statically typed language such as F# or Scala; the libraries can be substituted, the conciseness is comparable, but I have lots of python code which is as…
Li Haoyi
  • 15,330
  • 17
  • 80
  • 137
22
votes
2 answers

Is metaprogramming a subset of reflection?

I used to think that metaprogramming involved modifying the program, and (as do some answers to What is reflection and why is it useful? ) that reflection merely consisted of introspection of a program. However, the reflection tag wiki…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
22
votes
5 answers

How to set "dynamically" variable values?

I am using Ruby on Rails 3.0.9 and I am trying to set "dynamically" some variable values. That is... ... in my model file I have: attr_accessor :variable1, :variable2, :variable3 # The 'attributes' argument contains one or more symbols which name…
Backo
  • 18,291
  • 27
  • 103
  • 170
22
votes
1 answer

How do you list included Modules in a Ruby Class?

How would you list out the modules that have been included in a specific class in a class hierarchy in Ruby? Something like this: module SomeModule end class ParentModel < Object include SomeModule end class ChildModel < ParentModel end p…
Lance
  • 75,200
  • 93
  • 289
  • 503
22
votes
4 answers

1 class inherits 2 different metaclasses (abcmeta and user defined meta)

I have a class1 that needs to inherit from 2 different metaclasses which is Meta1 and abc.ABCMeta Current implementation: Implementation of Meta1: class Meta1(type): def __new__(cls, classname, parent, attr): new_class =…
momokjaaaaa
  • 1,293
  • 3
  • 17
  • 32
22
votes
5 answers

What was Tim Sweeney thinking? (How does this C++ parser work?)

Tim Sweeney of Epic MegaGames is the lead developer for Unreal and a programming language geek. Many years ago posted the following screen shot to VoodooExtreme: As a C++ programmer and Sweeney fan, I was captivated by this. It shows generic C++…
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
22
votes
7 answers

Python add to a function dynamically

how do i add code to an existing function, either before or after? for example, i have a class: class A(object): def test(self): print "here" how do i edit the class wit metaprogramming so that i do this class A(object): def…
Timmy
  • 12,468
  • 20
  • 77
  • 107
22
votes
12 answers

D Templates: Coolest Hack

What is the coolest somewhat practical metaprogramming hack you've done or seen done in the D programming language? Somewhat practical means excluding, for example, the compile-time raytracer.
dsimcha
  • 67,514
  • 53
  • 213
  • 334
22
votes
3 answers

Using SFINAE to check if the type is complete or not

Is it possible to check with SFINAE if the type is completely defined? E.g. template struct hash; template <> struct hash {}; // is_defined_hash_type definition... enum Enum { A, B, C, D }; static_assert ( …
Nikki Chumakov
  • 1,215
  • 8
  • 18
22
votes
5 answers

Why is Python's eval() rejecting this multiline string, and how can I fix it?

I am attempting to eval the following tab-indented string: '''for index in range(10): os.system("echo " + str(index) + "") ''' I get, "There was an error: invalid syntax , line 1" What is it complaining about? Do I need to indent to match…
Christos Hayward
  • 5,777
  • 17
  • 58
  • 113
21
votes
1 answer

Variadic variadic template templates

I'm currently struggling with the following code, the intent of which is to implement variadic variadic template templates: template < template class Head, template class... > struct…
kmore
  • 924
  • 9
  • 18