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

Running ruby method around existing specified method

I've got an existing library comprised of many services which all respond to the method execute each method does it's logic class BaseService def execute raise NotImplementedError end end class CreateUserService < BaseService def…
skukx
  • 617
  • 5
  • 15
3
votes
1 answer

How can I dynamically create a class based off ActiveRecord at runtime? (Ruby)

I'm experimenting with meta programming and want to dynamically create a class that inherits from ActiveRecord. For example, I can do this: Object.const_set("Orders", Class.new { def blah() 42 end }) So now I can: o = Orders.new o.blah #<==…
cbmeeks
  • 11,248
  • 22
  • 85
  • 136
3
votes
4 answers

Dynamic function resolution at runtime

My project needs to load many modules at runtime, and each one contains many functions with a form similar to the below pseudo code: void someFunction(Context &ctx) { bool result; result = ctx.call("someFunction2")(ctx.arg["arg1"],…
Iffi
  • 31
  • 4
3
votes
1 answer

How do I get the source for a Python class I declared programmatically?

I'm trying to use inspect.getsource() to get the source of a class that was defined like this: import inspect Cat = type('Cat', (), {}) def meow_local(self): print("meow") Cat.meow = meow_local print(inspect.getsource(Cat)) The error I get…
latj
  • 616
  • 1
  • 6
  • 23
3
votes
1 answer

how to check if a class has an operator []?

I'm trying to check if operator [] overloaded in some class. There is my code: template struct has_operator : std::false_type {}; template struct has_operator < T, …
3
votes
0 answers

When should I use optionally generated functions?

The manual says: Generated functions can achieve high efficiency at run time, but come with a compile time cost: a new function body must be generated for every combination of concrete argument types. Typically, Julia is able to compile "generic"…
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
3
votes
1 answer

Constexpr vs Template Metaprogramming - performance differences

I can see constexpr is winning more and more territory and one of the arguments for its use over template metaprogramming (TMP) is performance. My questions are: Are there any examples of performance comparisons in this regard? And why not replace…
3
votes
1 answer

Program to find what predicates are missing for a goal to be provable

The program at the bottom is supposed to return clauses that are missing for a goal, in this case p(1, 3, fire) to be provable. The problem is that it returns one solution “too many” as shown in the last solution in this output: ?- main. MISSING…
user3170496
3
votes
3 answers

Defining a method that uses an out-of-scope variable in Ruby

I want to make a Test::Unit test_helper method that I can call to wipe a bunch of tables after the tests execute. Here's the general idea I have: def self.wipe_models(*models) def teardown models.each do |model| model =…
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
3
votes
1 answer

Ruby metaprogramming question

When I call self.class.instance_variable_set("@var", ...) from inside a class method, where is that variable actually stored? Is it on the class itself? On the instance of that class? I can't seem to find it with any of the following: e =…
Dave
  • 4,356
  • 4
  • 37
  • 40
3
votes
1 answer

Determine short operators (+= like) usage in descriptor

I am now trying to create a descriptor class for model fields which saves it's modification history. I can determine the fact when some method is called on field value by just overriding getattr: def __getattr__(self, attr): print(attr) …
Pavel Shishmarev
  • 1,335
  • 9
  • 24
3
votes
2 answers

C++14 expand a tuple three by three

I have a C++14 tuple of size multiple of 3 and I would like to expand it 3 by 3 sequentially to a function. tuple a(1, 2, 4, 6, 7, 2); void process_triplet(int& mystate, int a, int b, int c) { // do something on a b…
Jes
  • 2,614
  • 4
  • 25
  • 45
3
votes
1 answer

Fetch dynamically a defined constant

According to Ruby const_get's documentation, this method returns value for a constant. So to reproduce the following behavior: module A FOO = 42 module B def self.call FOO end end end A::B.call # => 42 I wrote this code: A =…
Zag zag..
  • 6,041
  • 6
  • 27
  • 36
3
votes
2 answers

Getting the whole AST of the file / complex code

Julia manual states: Every Julia program starts life as a string: julia> prog = "1 + 1" "1 + 1" I can easily get the AST of the simple expression, or even a function with the help of quote / code_*, or using Meta.parse / Meta.show_sexpr if I have…
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
3
votes
2 answers

How to create properties with "delegated" accessors?

I'm new to c# and have been puzzling over this for a couple of days. Basically I want to create a type of property with getter and setter logic delegated to a base type to which this parameter belongs. This is just one application: a property whose…
Axeman
  • 29,660
  • 2
  • 47
  • 102