metaprogramming

Equivalent of python eval in Haskell

懵懂的女人 提交于 2019-12-20 12:44:28
问题 There is function in python called eval that takes string input and evaluates it. >>> x = 1 >>> print eval('x+1') 2 >>> print eval('12 + 32') 44 >>> What is Haskell equivalent of eval function? 回答1: It is true that in Haskell, as in Java or C++ or similar languages, you can call out to the compiler, then dynamically load the code and execute it. However, this is generally heavy weight and almost never why people use eval() in other languages. People tend to use eval() in a language because

Compile-time 'String' Manipulation with Variadic Templates

做~自己de王妃 提交于 2019-12-20 12:38:20
问题 Hey all, I'm currently trying to write a compile-time string encryption (using the words 'string' and 'encryption' quite loosely) lib. What I have so far is as follows: // Cacluate narrow string length at compile-time template <char... ArgsT> struct CountArgs { template <char... ArgsInnerT> struct Counter; template <char Cur, char... Tail> struct Counter<Cur, Tail...> { static unsigned long const Value = Counter<Tail...>::Value + 1; }; template <char Cur> struct Counter<Cur> { static unsigned

Dynamically generate scopes in rails models

若如初见. 提交于 2019-12-20 12:19:40
问题 I'd like to generate scopes dynamically. Let's say I have the following model: class Product < ActiveRecord::Base POSSIBLE_SIZES = [:small, :medium, :large] scope :small, where(size: :small) scope :medium, where(size: :medium) scope :large, where(size: :large) end Can we replace the scope calls with something based on the POSSIBLE_SIZES constant? I think I'm violating DRY to repeat them. 回答1: you could do class Product < ActiveRecord::Base [:small, :medium, :large].each do |s| scope s, where

alias_method and class_methods don't mix?

◇◆丶佛笑我妖孽 提交于 2019-12-20 12:10:24
问题 I've been trying to tinker with a global Cache module, but I can't figure out why this isn't working. Does anyone have any suggestions? This is the error: NameError: undefined method `get' for module `Cache' from (irb):21:in `alias_method' ... generated by this code: module Cache def self.get puts "original" end end module Cache def self.get_modified puts "New get" end end def peek_a_boo Cache.module_eval do # make :get_not_modified alias_method :get_not_modified, :get alias_method :get, :get

Getting interface implementations in referenced assemblies with Roslyn

倾然丶 夕夏残阳落幕 提交于 2019-12-20 10:36:11
问题 I'd like to bypass some classical assembly scanning techniques in a framework I am developing. So, say I've defined the following contract: public interface IModule { } This exists in say Contracts.dll . Now, if I want to discover all implementations of this interface, we would probably do something similar to the following: public IEnumerable<IModule> DiscoverModules() { var contractType = typeof(IModule); var assemblies = AppDomain.Current.GetAssemblies() // Bad but will do var types =

How to pass arguments to the metaclass from the class definition in Python 3.x?

五迷三道 提交于 2019-12-20 10:34:53
问题 This is a Python 3.x version of the How to pass arguments to the metaclass from the class definition? question, listed separately by request since the answer is significantly different from Python 2.x. In Python 3.x, how do I pass arguments to a metaclass's __prepare__ , __new__ , and __init__ functions so a class author can give input to the metaclass on how the class should be created? As my use case, I'm using metaclasses to enable automatic registration of classes and their subclasses

Dynamically defined setter methods using define_method?

被刻印的时光 ゝ 提交于 2019-12-20 10:30:56
问题 I use a lot of iterations to define convenience methods in my models, stuff like: PET_NAMES.each do |pn| define_method(pn) do ... ... end but I've never been able to dynamically define setter methods, ie: def pet_name=(name) ... end using define_method like so: define_method("pet_name=(name)") do ... end Any ideas? Thanks in advance. 回答1: Here's a fairly full example of using define_method in a module that you use to extend your class: module VerboseSetter def make_verbose_setter(*names)

Downcasting from base pointer to templated derived types

*爱你&永不变心* 提交于 2019-12-20 09:25:22
问题 I have the following hierarchy: class base { public: virtual ~base(){} virtual void foo() {} }; template <typename T> class derived1 : public base { virtual void foo() {}; }; template <typename T> class derived2 : public base { virtual void foo() {}; }; Now given a pointer to base, I'd like to find out if the underlying is either derived1 or derived2. The problem is that both derived1 and derived2 can be specialised on many different types, using dynamic_cast to test for a down cast requires

What does “typename =” mean in the template parameters?

前提是你 提交于 2019-12-20 09:11:39
问题 I have seen this expression in page 189 of the book "Effective Modern C++": template<typename T, typename = typename std::enable_if<condition>::type> explicit Person(T&& n); I am just wondering what does the part " typename = " mean. It certainly looks like a default argument for a template parameter. But don't you need something like " typename some_name = ... " in a default argument? There is no name for the second template argument, and I don't see the second template argument being used

`respond_to?` vs. `respond_to_missing?`

陌路散爱 提交于 2019-12-20 08:56:08
问题 What is the point of defining respond_to_missing? as opposed to defining respond_to? ? What goes wrong if you redefine respond_to? for some class? 回答1: Without respond_to_missing? defined, trying to get the method via method will fail: class Foo def method_missing name, *args p args end def respond_to? name, include_private = false true end end f = Foo.new f.bar #=> [] f.respond_to? :bar #=> true f.method :bar # NameError: undefined method `bar' for class `Foo' class Foo def respond_to? *args