metaprogramming

Avoiding class_eval in Ruby metaprogramming

I want to have a return_empty_set class method in Ruby, similar to the attr_reader methods. My proposed implementation is class Class def return_empty_set *list list.each do |x| class_eval "def #{x}; Set.new; end" end end end and example usage: class Foo return_empty_set :one end Foo.new.one # returns #<Set: {}> ...

In Ruby or Python can the very idea of Class be rewritten?

Howdy All... first time at stack overflow. I'm looking into using some of the metaprogramming features provided by Ruby or Python, but first I need to know the extent to which they will allow me to extend the language. The main thing I need to be able to do is to rewrite the concept of Class. This doesn't mean that I want to rewrite a...

How do I write an RSpec test to unit-test this interesting metaprogramming code?

Here's some simple code that, for each argument specified, will add specific get/set methods named after that argument. If you write attr_option :foo, :bar, then you will see #foo/foo= and #bar/bar= instance methods on Config: module Configurator class Config def initialize() @options = {} end def self.attr_option(*...

C# Generic Generics (A Serious Question)

In C# I am trying to write code where I would be creating a Func delegate which is in itself generic. For example the following (non-Generic) delegate is returning an arbitrary string: Func<string> getString = () => "Hello!"; I on the other hand want to create a generic which acts similarly to generic methods. For example if I want ...

How can one make a 'passthru' function in C++ using macros or metaprogramming?

So I have a series of global functions, say: foo_f1(int a, int b, char *c); foo_f2(int a); foo_f3(char *a); I want to make a C++ wrapper around these, something like: MyFoo::f1(int a, int b, char* c); MyFoo::f2(int a); MyFoo::f3(char* a); There's about 40 functions like this, 35 of them I just want to pass through to the global fu...

grails metaprogramming

Hi, My understanding is that there are two obvious places in a Grails app where one can do meta-programming: The init closure of Bootstrap.groovy The doWithDynamicMethods closure of a plugin The meta-programming I'm referring to here should be visible throughout the metaprogramming, typical examples include adding (or replacing) met...

Unified data representation and form generation in PHP

Hi, I'd want to start using a new system for this project and I was looking for some tools to achieve my goals. Those would be: Data represented under only one form ( only the database schema, or the database itself or whatever) Possibility to easily generate and update other data representations (ex: SQL code & forms from DB schema) ...

Good real-world uses of metaclasses (e.g. in Python)

I'm learning about metaclasses in Python. I think it is a very powerful technique, and I'm looking for good uses for them. I'd like some feedback of good useful real-world examples of using metaclasses. I'm not looking for example code on how to write a metaclass (there are plenty examples of useless metaclasses out there), but real exam...

Is it possible in Ruby to define a method which name ends with : (colon)?

Just wondering if it is possible, by some loophole, to define a method name that ends in a colon. The purpose it to make things look like this: mymethod: arg1,arg2,arg3 ...

How do I temporarily monkey with a global module constant?

Greetings, I want to tinker with the global memcache object, and I found the following problems. Cache is a constant Cache is a module I only want to modify the behavior of Cache globally for a small section of code for a possible major performance gain. Since Cache is a module, I can't re-assign it, or encapsulate it. I Would Lik...

alias_method and class_methods don't mix?

Greetings, I've been trying to tinker with a global Cache module, but I can't figure out why this isn't working. Does anyone have any suggestions? This is the error produced for the below code: NameError: undefined method get' for moduleCache' from (irb):21:in `alias_method' module Cache def self.get puts "original" ...

Ruby Metaprogramming

I'm trying to write a DSL that allows me to do Policy.name do author "Foo" reviewed_by "Bar" end The following code can almost process it: class Policy include Singleton def self.method_missing(name,&block) puts name puts "#{yield}" end def self.author(name) puts name end def self.reviewed_by(name) put...

C++ meta-splat function

Is there an existing function (in boost mpl or fusion) to splat meta-vector to variadic template arguments? For example: splat<vector<T1, T2, ...>, function>::type // that would be the same as function<T1, T2, ...> My search have not found one, and I do not want to reinvent one if it already exists. Alternatively, is there a solutio...

Checking if a function has C-linkage at compile-time [unsolvable]

Is there any way to check if a given function is declared with C-linkage (that is, with extern "C") at compile-time? I am developing a plugin system. Each plugin can supply factory functions to the plugin-loading code. However, this has to be done via name (and subsequent use of GetProcAddress or dlsym). This requires that the functions...

Given an instance of a Ruby object, how do I get its metaclass?

Normally, I might get the metaclass for a particular instance of a Ruby object with something like this: class C def metaclass class << self; self; end end end # This is this instance's metaclass. C.new.metaclass => #<Class:#<C:0x01234567>> # Successive invocations will have different metaclasses, # since they're different ins...

Calling different functions depending on the template parameter c++

I want to have something like that class A { public: Array& operator()() { . . . } }; class B { public: Element& operator[](int i) { ... } }; template<class T> class execute { public: output_type = operator()(T& t) { if(T == A) Array out = T()(); else { Array res;...

Ruby Module Inclusion in Methods

In class Foo I'd like to include method Bar under certain conditions: module Bar def some_method "orly" end end class Foo def initialize(some_condition) if !some_condition "bar" else class << self; include Bar; end end end end Is there any cleaner (and clearer) way to achieve the incl...

Programmatically create static arrays at compile time in C++

One can define a static array at compile time as follows: const std::size_t size = 5; unsigned int list[size] = { 1, 2, 3, 4, 5 }; Question 1 - Is it possible by using various kinds of metaprogramming techniques to assign these values "programmatically" at compile time? Question 2 - Assuming all the values in the array are to be ...

How to change verbose code in Ruby to simpler code using Metaprogramming?

It is said that Ruby is a metaprogramming language. I wonder if the first 3 lines can be made less verbose using metaprogramming? RUBY_VERSION ||= "unknown version" RUBY_PATCHLEVEL ||= "unknown patchlevel" RUBY_PLATFORM ||= "unknown platform" print "Ruby ", RUBY_VERSION, " patch ", RUBY_PATCHLEVEL, " on ", RUBY_PLATFORM ...

C++ compiler maximum number of classes

hello. In meta-programming number of classes grows quite fast. Is maximum number of classes modern compiler allows, for example g++, something to be concerned about? Thank you ...