metaprogramming

How do you debug heavily templated code in c++?

I find it very hard to figure out what is wrong with my code when using C++ template meta-programming. It might be that I am just not very good at understanding the error messages, but as far as I'm aware I can't resort to putting in print statements or breakpoints to figure out what's going on. What tips or advice can you offer when tr...

Dynamic properties for classes in visual basic

Hi, I am a vb.net newbie, so please bear with me. Is it possible to create properties (or attributes) for a class in visual basic (I am using Visual Basic 2005) ? All web searches for metaprogramming led me nowhere. Here is an example to clarify what I mean. public class GenericProps public sub new() ' ??? end sub p...

o.errors.allErrors.each { println it } by default when failing to save a domain object

When persisting domain objects using Grails/GORM I frequently find myself wondering why a save() call fails. This can easily be solved by adding the logic: if (!o.save()) { o.errors.allErrors.each { println it } } However, adding this everywhere I do a .save() adds a lot of duplicate code. In the spirit of DRY I'd like to configu...

'this' in Groovy meta-programming

Hi, In the following Groovy code snipped to add a fileAsString method to the String class, could someone explain what exactly 'this' refers to. I thought it was the object on which the fileAsString method is invoked, but apparently that's actually what delegate refers to. String.metaClass.fileAsString = { this.class.getResourceAsSt...

How to defn a function from string in Clojure?

Hi, I'd like to do this (in REPL or anywhere) (defn (symbol "print-string") [k] (println k)) and then be able to do (print-string "lol") Or, if there is any other way to create defn from custom strings in macroses, could you push me into the right direction please? ...

How to distinguish between a function and a class method ?

If a variable refers to either a function or a class method, how can I find out which one it is and get the class type in case it is a class method especially when the class is still being declared as in the given example. eg. def get_info(function_or_method) : print function_or_method class Foo(object): def _...

python decorators and methods

Hi. New here. Also I'm (very) new to python and trying to understand the following behavior. Can someone explain to me why the two methods in this example have different output? def map_children(method): def wrapper(self,*args,**kwargs): res = method(self,*args,**kwargs) for child in self._children: m...

Template Meta-programming with Char Arrays as Parameters.

I'm playing around with TMP in GCC 4.3.2's half-implementation of C++0x, and I was wondering if there was a way to somehow do the following: template <char x, char... c> struct mystruct { ... }; int main () { mystruct<"asdf">::go(); } It obviously won't let me do it just like that, and I thought I'd get lucky by using user-defin...

Purdue Compiler Construction Tool Set (PCCTS) : website, tutorial, book?

I am a college student and have been asked to familiarize myself with PCCTS, Purdu Compiler Construction Tool Set. I have been given a link to http://www.polhode.com/pccts.html I have to code some basic program in PCCTS and later on use the knowledge in compiler optimization. As such there are a few results available on google for th...

Conditions for a meta-circular evaluator

Are there any conditions that a language must satisfy so that a meta-circular evaluator can be written for that language? Can I write one for BASIC, or for Python? ...

Reversible version of compile() in Python

I'm trying to make a function in Python that does the equivalent of compile(), but also lets me get the original string back. Let's call those two functions comp() and decomp(), for disambiguation purposes. That is, a = comp("2 * (3 + x)", "", "eval") eval(a, dict(x=3)) # => 12 decomp(a) # => "2 * (3 + x)" The returned string does not...

How to Dynamically Determine a Method Name in Ruby

In Ruby, is there a way to determine the name of a method, akin to how the "class" method returns an object's type? For example: def example_method puts method_name end #=> "example_method" ...

What’s the best way to discover all variables a Perl application has currently defined?

I am looking for best, easiest way to do something like: $var1="value"; bunch of code..... **print allVariablesAndTheirValuesCurrentlyDefined;** ...

Programmatically get values of variables local to function in javascript?

Given: function foo(){ var bar = "quux"; console.log(/*mystery code here*/); } I'm looking for code that when inserted into the comment would yield the value of bar. By way of illustration, something like this works in a global scope: var foo = "bar"; var bar = "quux"; console.log(window[foo]); But, of course, variables defined...

How do I use define_method to create class methods?

This is useful if you are trying to create class methods metaprogramatically: def self.create_methods(method_name) # To create instance methods: define_method method_name do ... end # To create class methods that refer to the args on create_methods: ??? end My answer to follow... ...

Is a compiletime constant index into a compiletime constant array itself compiletime constant?

I am trying to play fancy games which have the C++ compiler synthesize hash values of constant strings at compiletime. This would let me replace the string with a single identifier, with a massive savings in code size and complexity. For programming clarity and ease, it'd be awesome if I could examine and compute at compiletime with sim...

I need a serialization framework for D

I'm looking for a D template library to take an arbitrary variable and marshal it into a transportable bundle. The variable might be a basic value type (int, char[], real) or might be a struct or class and even might contain or be a reference type. A system that can do this without any per type help would be nice but I suspect that it's ...

Groovy expando metaclass

Hi, I've developed a Class that has some methods that augment Integer, it mainly lets me do this: def total = 100.dollars + 50.euros Now I have to extend Integer.metaClass doing something like this: Integer.metaClass.getDollars = {-> Money.Dollar(delegate) } I tried putting that at the bottom of the file, before the Money class...

Ruby metaprogramming online tutorial

I have just started to learn Ruby and got a good take on the basics. I keep hearing that one of the cool things that Ruby does very well is metaprogramming, but none of the tutorials I've read cover this. Searching Google I can only seem to find paid for ruby metaprogramming screen casts. So, where can I find a good Ruby metaprogramming...

ruby: can I have something like Class#inherited that's triggered only after the class definition?

#inherited is called right after the class Foo statement. I want something that'll run only after the end statement that closes the class declaration. Here's some code to exemplify what I need: class Class def inherited m puts "In #inherited for #{m}" end end class Foo puts "In Foo" end puts "I really wanted to have #inherit...