metaprogramming

Infix format for Nemerle macro

Say I need some very special multiplication operator. It may be implemented in following macro: macro @<<!(op1, op2) { <[ ( $op1 * $op2 ) ]> } And I can use it like def val = 2 <<! 3 And its work. But what I really want is some 'english'-like operator for the DSL Im developing now: macro @multiply(op1, op2) { <[ ( $op1 * ...

Where do you find templates useful?

At my workplace, we tend to use iostream, string, vector, map, and the odd algorithm or two. We haven't actually found many situations where template techniques were a best solution to a problem. What I am looking for here are ideas, and optionally sample code that shows how you used a template technique to create a new solution to a ...

Is metaprogramming possible in C#?

In particular, would it be possible to have code similar to this c++ code executed at compile time in c#? template <int N> struct Factorial { enum { value = N * Factorial<N - 1>::value }; }; template <> struct Factorial<0> { enum { value = 1 }; }; // Factorial<4>::value == 24 // Factorial<0>::value == 1 void foo() { int ...

What are the coolest examples of metaprogramming that you've seen in C++?

What are the coolest examples of metaprogramming that you've seen in C++? What are some practical uses of metaprogramming that you've seen in C++? ...

D Templates: Coolest Hack

What is the coolest somewhat practical metaprogramming hack you've done or seen done in the D programming language? Somewhat practical means excluding, for example, the compile-time raytracer. ...

Is anyone using D in commercial applications?

Ok, this is a little open ended, but I think D could do with a bit of promotion. Personally I think D is a superb implementation language - but it's not mainstream enough yet for many people to take it seriously. Since it's not commerically backed, the only way to change that is through community effort and visibility. So I'd really li...

How to use boost::mpl to compose policies?

I have used something like the following to compose policies for my application: The policy classes look like this: struct Policy { static void init(); static void cleanup(); //... }; template <class CarT, class CdrT> struct Cons { static void init() { CarT::init(); CdrT::init(); } static void cleanup() { CdrT:...

Python: wrapping method invocations with pre and post methods

I am instantiating a class A (which I am importing from somebody else, so I can't modify it) into my class X. Is there a way I can intercept or wrape calls to methods in A? I.e., in the code below can I call x.a.p1() and get the output X.pre A.p1 X.post Many TIA! class A: # in my real application, this is an imported class ...

Is there a way to do a C++ style compile-time assertion to determine machine's endianness?

I have some low level serialization code that is templated, and I need to know the system's endianness at compiletime obviously (because the templates specializes based on the system's endianness). Right now I have a header with some platform defines, but I'd rather have someway to make assertions about endianness with some templated ...

Dynamically update ModelForm's Meta class

I am hoping to dynamically update a ModelForm's inline Meta class from my view. Although this code seems to update the exclude list in the Meta class, the output from as_p(), as_ul(), etc does not reflect the updated Meta exclude. I assume then that the html is generated when the ModelForm is created not when the as_*() is called. Is th...

In Python, how can you get the name of a member function's class?

I have a function that takes another function as a parameter. If the function is a member of a class, I need to find the name of that class. E.g. def analyser(testFunc): print testFunc.__name__, 'belongs to the class, ... I thought testFunc.__class__ would solve my problems, but that just tells me that testFunc is a function....

Python decorator makes function forget that it belongs to a class

I am trying to write a decorator to do logging: def logger(myFunc): def new(*args, **keyargs): print 'Entering %s.%s' % (myFunc.im_class.__name__, myFunc.__name__) return myFunc(*args, **keyargs) return new class C(object): @logger def f(): pass C().f() I would like this to print: Entering C...

How to declare/define a class with template template parameters without using an extra template parameter

Consider the following use of template template parameters... #include <iostream> template <typename X> class A { X _t; public: A(X t) :_t(t) { } X GetValue() { return _t; } }; template <typename T, template <typename T> class C > class B { C<T> _c; public: B(T t) :_c(t) ...

RDoc : Change name of 'Atttributes:' section in ri-documentation

I'm using some meta-programming to generate a bunch of methods in ruby like so: class EmotionalObject def self.mood( name, *details ) define_method(name) do # ... end end mood :happy, #... mood :sad, #... mood :ebuillent, #... #... end I know that I can pass rdoc '-A mood' to get it to recognize my mood gen...

How can I dynamically get the set of classes from the current python module?

I have a python module that defines a number of classes: class A(object): def __call__(self): print "ran a" class B(object): def __call__(self): print "ran b" class C(object): def __call__(self): print "ran c" From within the module, how might I add an attribute that gives me all of the classes? ...

How do you structure your source code?

(I'm asking this with Visual Studio Web Applications in mind, but am interested in more high-concept answers) Recent topic of conversation in my life with colleagues and friends has been how best to structure source files within a project. Not a source-control question or a development-environment question per se, I'm more interested in...

How do you detect that monkey patching has occurred in Ruby?

How do you check that monkey patching has been done to a specific class in Ruby? If that is possible, is it also possible to get the previous implementation(s) of the attribute that's been patched? ...

Difference between "class << anObject" and anObject.class_eval

I see the following code in the attribute_fu plugin: module AttributeFu module Associations #:nodoc: def self.included(base) #:nodoc: ...

Is there a way to use annotations in Java to replace accessors?

I'm a little new to the Java 5 annotations and I'm curious if either of these are possible: This annotation would generate a simple getter and setter for you. @attribute private String var = ""; The @NotNull annotation indicates that a variable connot be null so you don't have to write that boilerplate code every time. /* * @param ...

Descendant Enumeration in Objective-C

Is it possible to get a list of all descendant classes of a particular class in objective-c? Something like: @interface A : NSObject @end @interface B : A @end @interface C : A @end NSArray *descendants = [A allDescendants]; // descendants = [B, C] ...