metaprogramming

Ruby: Inherit code that works with class variables

拜拜、爱过 提交于 2020-04-23 10:17:32
问题 The situation: I have multiple classes that should each hold a variable with a configuration hash; a different hash for each class but the same for all instances of a class. At first, i tried like this class A def self.init config @@config = config end def config @@config end end class B < A; end class C < A; end But soon noticed that it wouldn't work that way because @@config is held in the context of A, not B or C, thus: B.init "bar" p B.new.config # => "bar" p C.new.config # => "bar" -

Ruby: How to chain multiple method calls together with “send”

淺唱寂寞╮ 提交于 2020-04-07 14:28:39
问题 There has to be a built in way of doing this, right? class Object def send_chain(arr) o=self arr.each{|a| o=o.send(a) } return o end end 回答1: I just ran across this and it really begs for inject: def send_chain(arr) arr.inject(self) {|o, a| o.send(a) } end 回答2: No, there isn't a built in way to do this. What you did is simple and concise enough, not to mention dangerous. Be careful when using it. On another thought, this can be extended to accept arguments as well: class Object def send_chain

'respond_to?' versus 'defined?'

安稳与你 提交于 2020-03-22 03:52:56
问题 If I want to check whether a method with a given name is defined, which is better to use, respond_to? , or defined? ? From the point of view of efficiency, there can be an argument for using defined? because defined? is a built in keyword, whereas respond_to? is a method, and hence the former might be faster. But on the other hand, under the situation that the expression to be checked is known to be a simple method, defined? needs to parse the whole expression, and that may be a drawback as

Iterating over different functions with different number of parameters in Julia

主宰稳场 提交于 2020-02-24 14:43:02
问题 I'm trying to run a loop over different functions with different number of arguments. The variables are created at runtime inside the loop, and I want to use eval at each iteration to instantiate a Struct using the variable :symbol. However, I can't do this since eval only works in the global scope. This is the MWE for the case that works: function f1(x); return x; end function f2(x1,x2); return x1+x2; end handles = [f1,f2] args =[:(x1),:(x1,x2)] x1 = 1; x2 = 1; for (i,f) in enumerate(handles

Iterating over different functions with different number of parameters in Julia

半腔热情 提交于 2020-02-24 14:42:31
问题 I'm trying to run a loop over different functions with different number of arguments. The variables are created at runtime inside the loop, and I want to use eval at each iteration to instantiate a Struct using the variable :symbol. However, I can't do this since eval only works in the global scope. This is the MWE for the case that works: function f1(x); return x; end function f2(x1,x2); return x1+x2; end handles = [f1,f2] args =[:(x1),:(x1,x2)] x1 = 1; x2 = 1; for (i,f) in enumerate(handles

Execute function inside function template only for those types that have the function defined

核能气质少年 提交于 2020-02-21 09:55:30
问题 I have a function template which takes many different types as it's input. Out of those types only one of them has a getInt() function. Hence I want the code to run the function only for that type. Please suggest a solution. Thanks #include <type_traits> #include <typeinfo> class X { public: int getInt(){ return 9; } }; class Y{ }; template<typename T> void f(T& v){ // error: 'class Y' has no member named 'getInt' // also tried std::is_same<T, X>::value if(typeid(T).name() == typeid(X).name()

What is the purpose of C++20 std::common_reference?

我与影子孤独终老i 提交于 2020-02-17 07:00:31
问题 C++20 introduces std::common_reference. What is its purpose? Can someone give an example of using it? 回答1: common_reference came out of my efforts to come up with a conceptualization of STL's iterators that accommodates proxy iterators. In the STL, iterators have two associated types of particular interest: reference and value_type . The former is the return type of the iterator's operator* , and the value_type is the (non-const, non-reference) type of the elements of the sequence. Generic

What is the purpose of C++20 std::common_reference?

走远了吗. 提交于 2020-02-17 06:57:10
问题 C++20 introduces std::common_reference. What is its purpose? Can someone give an example of using it? 回答1: common_reference came out of my efforts to come up with a conceptualization of STL's iterators that accommodates proxy iterators. In the STL, iterators have two associated types of particular interest: reference and value_type . The former is the return type of the iterator's operator* , and the value_type is the (non-const, non-reference) type of the elements of the sequence. Generic

C++ template meta-programming, number of member variables?

江枫思渺然 提交于 2020-02-14 05:45:52
问题 Is it possible in C++ to determine number of variables/fields in the generic class? for example // suppose I need metaclass number_members determines number of members struct example { int i, j; }; assert(number_members<example>::value==2); I looked through mpl but could not find implementation. thanks. 回答1: No. C++ does not provide general introspection into structures. You can try a C++0x std::tuple, which has some of the features of a general POD struct . Or, try to roll your own from the

C++ template meta-programming, number of member variables?

一曲冷凌霜 提交于 2020-02-14 05:44:51
问题 Is it possible in C++ to determine number of variables/fields in the generic class? for example // suppose I need metaclass number_members determines number of members struct example { int i, j; }; assert(number_members<example>::value==2); I looked through mpl but could not find implementation. thanks. 回答1: No. C++ does not provide general introspection into structures. You can try a C++0x std::tuple, which has some of the features of a general POD struct . Or, try to roll your own from the