How Pony (ORM) does its tricks?

Pony ORM author is here. Pony translates Python generator into SQL query in three steps: Decompiling of generator bytecode and rebuilding generator AST (abstract syntax tree) Translation of Python AST into “abstract SQL” — universal list-based representation of a SQL query Converting abstract SQL representation into specific database-dependent SQL dialect The most complex part is … Read more

How to drive C#, C++ or Java compiler to compute 1+2+3+…+1000 at compile time?

Updated Now with improved recursion depth! Works on MSVC10 and GCC without increased depth. 🙂 Simple compile-time recursion + addition: template<unsigned Cur, unsigned Goal> struct adder{ static unsigned const sub_goal = (Cur + Goal) / 2; static unsigned const tmp = adder<Cur, sub_goal>::value; static unsigned const value = tmp + adder<sub_goal+1, Goal>::value; }; template<unsigned Goal> … Read more

What does send() do in Ruby?

send sends a message to an object instance and its ancestors in class hierarchy until some method reacts (because its name matches the first argument). Practically speaking, those lines are equivalent: 1.send ‘+’, 2 1.+(2) 1 + 2 Note that send bypasses visibility checks, so that you can call private methods, too (useful for unit … Read more

Is it possible to figure out the parameter type and return type of a lambda?

Funny, I’ve just written a function_traits implementation based on Specializing a template on a lambda in C++0x which can give the parameter types. The trick, as described in the answer in that question, is to use the decltype of the lambda’s operator(). template <typename T> struct function_traits : public function_traits<decltype(&T::operator())> {}; // For generic types, … Read more

Can a line of Python code know its indentation nesting level?

If you want indentation in terms of nesting level rather than spaces and tabs, things get tricky. For example, in the following code: if True: print( get_nesting_level()) the call to get_nesting_level is actually nested one level deep, despite the fact that there is no leading whitespace on the line of the get_nesting_level call. Meanwhile, in … Read more

Conveniently Declaring Compile-Time Strings in C++

I haven’t seen anything to match the elegance of Scott Schurr’s str_const presented at C++ Now 2012. It does require constexpr though. Here’s how you can use it, and what it can do: int main() { constexpr str_const my_string = “Hello, world!”; static_assert(my_string.size() == 13, “”); static_assert(my_string[4] == ‘o’, “”); constexpr str_const my_other_string = my_string; … Read more

How do you pass arguments to define_method?

The block that you pass to define_method can include some parameters. That’s how your defined method accepts arguments. When you define a method you’re really just nicknaming the block and keeping a reference to it in the class. The parameters come with the block. So: define_method(:say_hi) { |other| puts “Hi, ” + other }

Is it possible to implement dynamic getters/setters in JavaScript?

This changed as of the ES2015 (aka “ES6”) specification: JavaScript now has proxies. Proxies let you create objects that are true proxies for (facades on) other objects. Here’s a simple example that turns any property values that are strings to all caps on retrieval, and returns “missing” instead of undefined for a property that doesn’t … Read more