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

Racket "eval" a datum

I'm trying to level up on metaprogramming in Racket and realized I don't know how to take a datum and simply "eval" it. If I have (for ((x '(("Five" (+ 2 3)) ("Twelve" (* 6 2)) ("Three" (- (/ 21 3) 4))))) (displayln…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
3
votes
0 answers

Annotation processor and anonymous classes

I'm trying to instrument some anonymous classes. For that I created an annotation: @Target(ElementType.TYPE_USE) @Retention(RetentionPolicy.SOURCE) public @interface Ann { } An annotation…
thewolf
  • 417
  • 1
  • 5
  • 10
3
votes
1 answer

Python: why do I need super().__init__() call in metaclasses?

I have got one question: why do I need to call super().--init--() in metaclasses? Because metaclass is factory of classes, I think we don`t need to call initialization for making objects of class Shop. Or with using super().--init-- we initializing…
F1lippun
  • 33
  • 2
3
votes
1 answer

Test for existence of std::ostream operator<< via SFINAE GCC bug?

I decided to try my own hand at a bit of Substitution Failure Is Not A Error (SFINAE) code to test if the global operator<< is defined for a custom type. The Stack Overflow question SFINAE + sizeof = detect if expression compiles already addresses…
McKay.CPP
  • 440
  • 1
  • 3
  • 11
3
votes
2 answers

Variadic template as first arguments

I want to write a generic template function that accepts and calls a number of different functions and performs additional setup and teardown operations. The functions signatures differ in the first arguments, such as: void foo(int i, void* self,…
Rolle
  • 2,900
  • 5
  • 36
  • 40
3
votes
3 answers

Template metaprogramming to match function signature exactly

With template meta-programming it's possible to query properties of types. For example in the case below I'm checking if a certain type has a member function "foo" which takes in another type as the argument. Is there a way to check if the parameter…
user3882729
  • 1,339
  • 8
  • 11
3
votes
1 answer

Is there delayed set in Julia? (equivalent of := from Mathematica)

I want to call something like rand((0, 1), N) (with N some integer assigned previously) many times in different parts of a program (all occurrences of which I might change in the future to, for example, rand((-1, 1), N) or randn(N)). How can I…
3
votes
1 answer

In typescript, can I remove the undefined and null types from an object using a list of keys?

I have created a function that does the runtime requirement of checking for null and undefined not being true: function hasFields(obj: T | T[], ...fields: (keyof T)[]): boolean { const inObj: { (obj: T): boolean } = (obj) =>…
Jeffrey Drake
  • 805
  • 10
  • 26
3
votes
0 answers

Create Functions via static Metaprogramming in Java/Kotlin

I have got a class with approximately 40 member-variables. Now I want to create getters and setters for them (they need to fire a kind of PropertyChangeEvent and getters aren't the default ones too; but the code is practically the same for each…
dCSeven
  • 815
  • 8
  • 18
3
votes
0 answers

Works fine in Visual Studio, fails to compile on G++ 9.2

The following code compiles fine with the latest version of Visual Studio Comumnity 16.5.4 configured to use the latest version of the C++ standard however it fails to compile on G++ 9.2 with a very cryptic and unhelpful error message. #include…
3
votes
1 answer

How does column definition in the blocks in rails migrations work?

Throughout Rails there is an idiom to query parameters through a block-variable. For instance in migrations. A typical migration in Rails looks something like this class CreateUsers < ActiveRecord::Migration[5.0] def change create_table :users…
von spotz
  • 875
  • 7
  • 17
3
votes
3 answers

Detecting duplicate work at compile time within C++ code

lets consider the following code: struct Foo { Foo(Bar b1, Bar b2) : b1(b1), b2(b2) {} void work() { b1.work(); b2.work(); //something } Bar& b1; Bar& b2; }; struct Bar { void work() { /* something…
Ikaros
  • 395
  • 1
  • 10
3
votes
1 answer

C++ Template Meta-programming, with variadic templates to perform an operation of specific members of a structure

I devoloped this method whilst finding a way to nicely abstract binding a struct to a SQL statement for a SQLite wrapper, my aim is to be able to abstract away most of the binding process as well as being able to "alias" the specialised function…
c0rp3n
  • 91
  • 7
3
votes
3 answers

Boost MPL to generate code for object serialization?

I want to generate serialization/deserialization code for class Object { string a; int b; long c; char d; }; by looking at a mpl sequence, but I need to be able to identify object and retrieve it back as well, I can't figure out…
3
votes
1 answer

Julia create multiple slightly modified versions of a function

I have a function that looks like function eom!(du, u, p) @views a, b = u[:,1], u[:,2]; @views da, db = du[:,1], du[:,2]; y = # some stuff involving p and a; da .= f(a, b, y); db .= g(b, a); end I now want to create a second a…
Grayscale
  • 1,462
  • 1
  • 13
  • 20