metaprogramming

Folding over arbitrarily many variadic packs

依然范特西╮ 提交于 2019-12-21 18:29:15
问题 I'm reading through Eric Niebler's post on his tiny metaprogramming library. In trying to implement the pieces that he omits / lists as challenges, I am left with the following implementation of transform : template <typename F, typename... As> using meta_apply = typename F::template apply<As...>; template <typename... > struct typelist_transform; // unary template <typename... T, typename F> struct typelist_transform<typelist<T...>, F> { using type = typelist<meta_apply<F,T>...>; }; //

variable name introspection in Python

試著忘記壹切 提交于 2019-12-21 17:48:15
问题 Is it possible to dynamically determine the name of a variable in Python? For example, I sometimes have the following situation: name = foo if bar else baz type = alpha or bravo D = { "name": name, "type": type } It would be nice if duplication there could be reduced with something like D = makedict(name, type) . Somewhat relatedly, it would sometimes be helpful for a function to know its own name: class Item(object): def item_create(self, item): dispatch("create", item) def item_delete(self,

Boost.MPL and type list generation

家住魔仙堡 提交于 2019-12-21 12:39:26
问题 Background This is for a memory manager in a game engine. I have a freelist implemented, and would like to have a compile-time list if these. (A MPL or Fusion vector, for example). The freelist 's correspond to allocation sizes, and when allocating/deallocating objects of size less than a constant, they will go to the corresponding freelist . In the end, this means small objects globally have amortized constant time allocation and constant time deallocation. (Yay.) Problem The problem is

Metaprogramming: How to discover the real class of an object?

你说的曾经没有我的故事 提交于 2019-12-21 12:23:03
问题 I was kidding with metaprogramming in Ruby and I did this code: class Class def ===(other) other.kind_of?(self) end end class FakeClass def initialize(object) methods.each {|m| eval "undef #{m}" if m.to_sym != :methods } define = proc do |m| eval(<<-END) def #{m}(*a, &b) @object.#{m}(*a, &b) rescue Object raise $!.class, $!.message.gsub("FakeClass", @object.class.to_s), $!.backtrace-[$!.backtrace[-caller.size-1]] end END end object.methods.each {|m| define[m] } def method_missing(name, *a, &b

Why can't I use a Perl variable's value to access a lexical variable name?

浪子不回头ぞ 提交于 2019-12-21 12:23:00
问题 Why does this print 42: $answer = 42; $variable = "answer"; print ${$variable} . "\n"; but this doesn't: my $answer = 42; my $variable = "answer"; print ${$variable} . "\n"; 回答1: Only package variables (the kind declared in your first example) can be targeted via symbolic references. Lexical ( my ) variables, cannot be, which is why your second example fails. See the excellent article Coping with Scoping for how the two separate variable systems in Perl operate. And see the also excellent Why

call before methods in model on ruby

偶尔善良 提交于 2019-12-21 12:10:24
问题 This my implementation to developing way to run code before all method in your model The call "before_hook :months_used" method need to be on bottom of class to the ExecutionHooks can get the instance_method loaded in the module. I would like to load the instance methods on top class BalanceChart < BalanceFind include ExecutionHooks attr_reader :options def initialize(options = {}) @options = options @begin_at = @options[:begin_at] end def months_used range.map{|date| I18n.l date, format:

interface paradigm performance (dynamic binding vs. generic programming)

时光怂恿深爱的人放手 提交于 2019-12-21 11:29:46
问题 While at their core dynamic binding and templates are fundamentally different things, they can be used to implement the same functionality. Code example (only for reference) A) dynamic binding namespace DB { // interface class CustomCode { public: virtual void operator()(char) const = 0; }; class Lib { public: void feature(CustomCode const& c) { c('d'); } }; // user code class MyCode1 : public CustomCode { public: void operator()(char i) const { std::cout << "1: " << i << std::endl; } };

What is the point of Ruby's method unbinding mechanism?

折月煮酒 提交于 2019-12-21 10:33:26
问题 Method#unbind returns an UnboundMethod reference to the method, which can later be bound to another object using UnboundMethod#bind. class Foo attr_reader :baz def initialize(baz) @baz = baz end end class Bar def initialize(baz) @baz = baz end end f = Foo.new(:test1) g = Foo.new(:test2) h = Bar.new(:test3) f.method(:baz).unbind.bind(g).call # => :test2 f.method(:baz).unbind.bind(h).call # => TypeError: bind argument must be an instance of Foo Initially, I thought this is incredibly awesome,

Creating dynamic docstrings in Python descriptor

人走茶凉 提交于 2019-12-21 09:33:06
问题 I am trying to generate some class definitions dynamically (for wrapping a C++ extension). The following descriptor works fine except when I try to access the docstring for a field using help(), it gives default documentation for the descriptor rather than the field it self. However when I do help(classname), it retrieves the docstring passed to the descriptor: class FieldDescriptor(object): def __init__(self, name, doc='No documentation available.'): self.name = name self.__doc__ = doc def _

When to favor untyped over typed quotations in F#?

送分小仙女□ 提交于 2019-12-21 09:02:12
问题 F# has both typed and untyped code quotations and I wonder what are the use cases where one would choose one over the other? Is the distinction just convenience and untyped and typed quotations are convertible to each in all cases or are typed quotations e. g. a subset of the ones possible with untyped quotations? Are there any examples which only work with typed, but not with untyped quotations – or the other way around? 回答1: In general, I'd recommend using typed quotations whenever you can.