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
2 answers

How to find all combinations of variadic typelist in C++

I would like to write a C++ metafunction that given a list of types, with no duplicated elements, generate all the possible sublists with size sizeof...(list) - 1. Update: I am looking for a generic implementation of the metafunction find_all, as…
3
votes
1 answer

Enforcing abstractmethod behavior when decorating all methods in an ABCMeta subclass

I want to implement a metaclass for wrapping methods to log additional information. But I also need to have abstractmethods. I tried to extend ABCMeta but it doesn't seem to enforce the @abstractmethod decorator: import types import abc def…
3
votes
3 answers

How can I get access to this nested template parameter?

Here is my code: template< template typename V, typename... Filtered> constexpr auto filter() { if constexpr(sizeof...(TSEvents) == 0) { return type_list{}; …
3
votes
1 answer

Making a function that takes a function call as an argument into a pipeable function.

require(magrittr) require(purrr) is.out.same <- function(.call, ...) { ## Checks if args in .call will produce identical output in other functions call <- substitute(.call) # Captures function call f_names <-…
3
votes
1 answer

Overwrite a metaClass property in Groovy

I would like to overwrite a dynamic property on a metaClass but it's not working as I would expect. Here is a simple example that demonstrate the problem. You can run it online at Groovy web console class Bike {} class Bell { def ring() {…
Jan Krakora
  • 2,500
  • 3
  • 25
  • 52
3
votes
1 answer

Etymology of reflection?

I've never found a clear explanation for the etymology of reflection in the context of computer languages, so I want to clarify this here. "Reflection" originates from Latin and has the following definitions: bend back turn back turn round So…
Roland
  • 7,525
  • 13
  • 61
  • 124
3
votes
1 answer

Invoke a private macro within a quote block

I'm trying to invoke a private macro, within a quote block, using a variable defined within the code block itself. This is the pseudo-code showing what I would like to do (doesn't work) defmodule Foo do defmacrop debug(msg) do quote…
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
3
votes
1 answer

Python magic meta class creation

Basically, I want to create a "template" type-object with staticmethods and then create a new object that inherits from it but uses the parent's methods using functools.partial where the arguments of the child are used. The child's arguments have…
Skorpeo
  • 2,362
  • 2
  • 15
  • 20
3
votes
1 answer

template parameters in templated type function

The following code does not compile with gcc 6.4 in Debian Sid, due to the error: "typename is not allowed". struct A { template static R f(T x) { return (R)x; } }; template R…
3
votes
2 answers

argument binding for dynamic __init_subclass__ method

I am trying to get a class decorator working. The decorator will add a __init_subclass__ method to the class it is applied to. However, when the method is added to the class dynamically, the first argument is not being bound to the child class…
Rick
  • 43,029
  • 15
  • 76
  • 119
3
votes
2 answers

Expected a type, got a template

I'm writing a sort of "asynchronous factory" where time-consuming constructions of concrete objects are deferred to std::async tasks. Each AsyncFactory will store a smart pointer to the object. [This is not the most correct application of Factory…
Patrizio Bertoni
  • 2,582
  • 31
  • 43
3
votes
6 answers

Is it possible to load one of two different classes in Java with the same name?

I have a lot of code that calls static methods on Foo like "Foo.method()". I have two different implementations of Foo and would like to use one or the other depending on the circumstances. In psuedocode: File Foo1.java class Foo1 implements Foo { …
Steve
  • 31
  • 2
3
votes
1 answer

Runtime generation and compilation of Cython functions

A brief version Is there an easy way to compile a Cython function in runtime given the code of the function as a string? In details I have a parametrized subroutine e.g. cdef algo(x, params) the algorithm performs a relatively small number of…
Maxim
  • 61
  • 5
3
votes
1 answer

Ruby generate a lambda programmatically

I am trying to generate a lambda programmatically. This is an example: This is the matrix that I need to generate: m = [[a(t), b(t)], [c(t), d(t)]] This is a time dependent matrix. If the a, b, c, d functions were always the same I would…
Rojj
  • 1,170
  • 1
  • 12
  • 32
3
votes
1 answer

How Can I Prevent Template Generation for Objects That Don't Implement a Method

So for the purpose of example let's say I have 3 simple structs, the second of which doesn't contain a bar method: struct one { void foo(const int); void bar(); }; struct two { void foo(const int); }; struct three { void foo(const…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288