metaprogramming

Defining a new logical operator in Ruby

时光毁灭记忆、已成空白 提交于 2019-12-23 09:55:59
问题 Very much an idle day-dream this but is it possible with some neat meta-programming trick to define a new logical operator in Ruby? I'd like to define a but operator. For example, if I want to do something if x but not y is true I have to write something like: if x and not y But I would like to write if x but not y It should work exactly the same as and but would be down to the programmer to use sensibly to increase the legibility of code. 回答1: Without editing the Ruby parser and sources and

Groovy: How to set a property within setProperty() and avoid infinite recursion?

左心房为你撑大大i 提交于 2019-12-23 09:47:35
问题 I'm trying to implement a domain class that records when any property's value was changed, but my setProperty() call results in infinite recursion when setting the actual value. This is how it looks right now: void setProperty(String name, value) { if(name == "modified") { this.modified = value return } else { if(this[name]==value) { return } this.modified = true this[name]=value } } So how can I access a property given its name without triggering a recursive setProperty() call? Or is there a

Debugging metaprograms

偶尔善良 提交于 2019-12-23 09:14:02
问题 Is there any way to check step by step what's going on in let's say template? I mean how it is instantiated step by step and so on? In book I've mentioned here , I found (2 minutes ago) quite interesting example of how binary could be implemented as a metafunction. template <unsigned long N> struct binary { static unsigned const value = binary<N/10>::value << 1 // prepend higher bits | N%10; // to lowest bit }; template <> // specialization struct binary<0> // terminates recursion { static

Force a specific overload when template template

早过忘川 提交于 2019-12-23 08:53:08
问题 Consider the following code : #include <iostream> #include <vector> #include <type_traits> // Version A template<typename T> void f(const T& x) { std::cout<<"Version A"<<std::endl; } // Version B template<template<typename> class T, typename T1> void f(const T<T1>& x) { std::cout<<"Version B"<<std::endl; } // Main int main(int argc, char* argv[]) { f(double()); f(std::vector<double>()); // <- How to force the use of version B ? return 0; } By default, it will produce : Version A Version A How

Add methods to controllers

纵然是瞬间 提交于 2019-12-23 05:32:08
问题 In a Grails application I would like to add a foo() method to all my controller classes. I know that I can do this inside a plugin's doWithDynamicMethods closure using code like: application.controllerClasses.toList()*.metaClass*.foo = { println 'foo called' } However, I don't want to create a plugin just for this purpose. Is there anywhere else I can do this. I suspect it might be possible within the init closure of BootStrap.groovy , but I don't know how to get access to the

How do I write my own loop_until?

血红的双手。 提交于 2019-12-23 05:29:22
问题 I'm practicing my Ruby meta-programming and trying to write my own loop method that will handle most of the ugliness in listening to a socket, but give the programmer the chance to specify the loop break condition and a block of things to do after each IO.select/sleep cycle. What I want to be able to write is something like this: x = 1 while_listening_until( x == 0 ) do x = rand(10) puts x end What I've been able to make work is: def while_listening_until( params, &block ) break_cond = params

is it possible to markup all programming languages under object oriented paradigm using a common markup schema?

邮差的信 提交于 2019-12-23 05:24:11
问题 i have planned to develop a tool that converts a program written in a programming language (eg: Java) to a common markup language (eg: XML) and that markup code is converted to another language (eg: C#) . in simple words, it is a programming language converter that converts program written in one language to another language. i think it is possible but i don know where to start. i wanna know the possibilities to do so and information about some existing system. 回答1: What you are trying to do

Template Factory Pattern in C++

隐身守侯 提交于 2019-12-23 04:03:33
问题 I am trying to make a factory that will have the type passed in, rather then having it hard coded for types. However, when I attempt to add the type to the factory inside of the types .cpp file, I will get a linker error. For example, he is a linker error I am currently getting. 1>RandomClass.obj : error LNK2019: unresolved external symbol "public: short __thiscall TemplatedFactory::AddType(char const *)" (??$AddType@VRandomClass@@@TemplatedFactory@@QAEFPBD@Z) referenced in function "void _

Creating a module for raising class-specific errors

六眼飞鱼酱① 提交于 2019-12-23 01:41:38
问题 In my rails projects, I often use this sort of behavior in my classes and models: class Whatever class WhateverError < StandardError; end def initialize(params={}) raise WhateverError.new("Bad params: #{params}") if condition # actual class code to follow end end The trouble is, this is both hugely repetitive and fairly verbose. I'd love it if I could just do this whenever I need to raise a class-specific error: class ErrorRaiser include ClassErrors def initialize(params={}) error("Bad params

Can one unroll a loop when working with an integer template parameter?

荒凉一梦 提交于 2019-12-22 14:09:19
问题 I have the following code: template <int size> inline uint hashfn( const char* pStr ) { uint result = *pStr; switch ( size ) { case 10: result *= 4; result += *pStr; case 9: result *= 4; result += *pStr; ... ... case 2: result *= 4; result += *pStr; } return result; } This code is a hash function for DNA sequences of certain lenghts where the length is a template parameter. It is an unrolled loop with a switch statement to jump in at the right spot. The size is however a constant since it is