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

R: Creating a custom error message giving argument values for a function in a package

Suppose there is a set of functions, drawn from a package not written by me, that I want to assign to a special behavior on error. My current concern is with the _impl family of functions in dplyr. Take mutate_impl, for example. When I get an error…
andrewH
  • 2,281
  • 2
  • 22
  • 32
3
votes
2 answers

How to use instance methods in python meta programming?

I have a class A: class A(object): a = 'a' def __init__(self): self.b = 'b' def foo(self): print A.a print self.b I just wondering how to construct this class by meta programming. I have tried: def Init(self): self.b = 'b'…
spring cc
  • 937
  • 1
  • 10
  • 19
3
votes
1 answer

Am I Allowed to Default a Template Argument in a Forward Declaration

So I'm trying to understand what's happening with Boost's ptree implementation. In ptree.hpp basic_ptree is actually defined: template class basic_ptree In ptree_fwd.hpp there is what looks like a forward…
3
votes
1 answer

Is it possible to get the name of a type alias in C++?

In C++, is it possible to get the name of a type alias ? For instance: using my_type = some_type; std::cout << get_type_name() << std::endl; Would print my_type, not some_type.
nicoulaj
  • 3,463
  • 4
  • 27
  • 32
3
votes
1 answer

How to programmatically define variables from string identifiers in Julia?

I want to set the string in the abc.conf file as the name of the variable, e.g., ProductNo, ProdName. Is this…
y6cmE
  • 43
  • 4
3
votes
3 answers

Define methods from template at runtime in ruby

Say I have the following classes: class Foo attr_accessor :name, :age end class Bar def initialize(name) @foo = Foo.new @foo.name = name end end I'd like to define an accessor on Bar that is simply an alias to foo.name.…
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
3
votes
3 answers

Is Curiously Recurring Template Pattern Implementation Specific?

So I've read through this: https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern And understand how Curiously Recurring Template Pattern (CRTP) works. But it seems like it depends upon the compiler implementation, specifically that the…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
1 answer

How to “splat” the function arguments for dynamically created function

I am generating some functions in the module: defmodule M do funs = [do_it: [:arg1, :arg2]] Enum.each(funs, fn {name, args} -> args = Enum.map(args, & {&1, [], Elixir}) def unquote(name)(unquote(args)), do:…
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
3
votes
4 answers

How do I replace a message body programmatically in Smalltalk?

As part of some testing, I'm looking to, temporarily, replace the contents of a message with one that will return a predictable set of test values. What is the standard Smalltalk-y way to do this type of work? Is there some sample code for me to…
blueberryfields
  • 45,910
  • 28
  • 89
  • 168
3
votes
1 answer

Get the class type of a static class function

I have a function pointer to a static class function Foo::bar(), and want to get the type of the class (Foo). Now, I know that I could get the class type if bar were a member function of Foo, instead of a static function, with something like the…
Michael
  • 273
  • 2
  • 10
3
votes
4 answers

Any way to generate repetitive operator<< definitions?

I have a few dozen of these types of structs and I'm hoping there is a clever way of generating the operator<< methods using macros or template meta-programming. Please also notice that endianess is also consideration and makes it a bit more…
chriskirk
  • 761
  • 1
  • 11
  • 22
3
votes
1 answer

Optional class-code generation in dependence of functions implemented by generics in haxe

Assuming I have the following classes in Haxe: class Pair { public var first:U = null; public var second:V = null; public function new(u:U, v:V) { this.first = u; this.second = v; } } class HashablePair
quant
  • 2,184
  • 2
  • 19
  • 29
3
votes
2 answers

loop unrolling and metaprogramming(TMP)?

While reading [Compile-time code optimization] from wikipedia' article-Template metaprogramming: template Vector& Vector::operator+=(const Vector& rhs) { for (int i = 0; i < length; ++i) value[i] +=…
Chen Li
  • 4,824
  • 3
  • 28
  • 55
3
votes
4 answers

Best way to create a "runner" script in Python?

I have a bunch of Python modules in a directory, all being a derivate class. I need a "runner" script that, for each module, instantiate the class that is inside it (the actual class name can be built by the module file name) and than call the "go"…
Claudio
  • 5,740
  • 5
  • 33
  • 40
3
votes
2 answers

What are alternatives to this typelist-based class hierarchy generation code?

I'm working with a simple object model in which objects can implement interfaces to provide optional functionality. At it's heart, an object has to implement a getInterface method which is given a (unique) interface ID. The method then returns a…
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207