标签: metaprogramming

使用 setter 和 getter 的动态方式

你好我的问题很简单我有简化的代码看起来像这样

foo = OpenStruct.new
bar = OpenStruct.new

mappings.each do |k,v|
 eval "foo.#{k} = bar.#{v}"
end
Run Code Online (Sandbox Code Playgroud)

我正在尝试修复这种eval用法而不是使用它 :) 找出了正确的部分,但我不知道如何分配给左部分。要访问 getter,您可以编写。

 bar.send(v.to_sym)
 # not to sure how to access setter for foo
Run Code Online (Sandbox Code Playgroud)

我正在使用Ruby 1.8.7,谢谢

ruby metaprogramming accessor

1
推荐指数
1
解决办法
575
查看次数

如何为 Ruby 中的所有实例设置默认(而不是 nil)变量初始化

实例变量在哪里第一次初始化为 nil?我可以将所有实例的默认值重新定义为其他值吗?

例如:

class Class
  #some code here or maybe in an Object class
end

class Foo1
  attr_accessor :bar
end

class Foo2
  attr_accessor :bar
end

p Foo1.new.bar # result is not nil
p Foo2.new.bar # result is not nil
Run Code Online (Sandbox Code Playgroud)

这可以通过修改阅读器来完成:

class Class
  def attr_accessor(attr_name)
    ...
    define_method "#{attr_name}" do
      if instance_variable_get "@#{attr_name}_history"
        instance_variable_get "@#{attr_name}_history"
      else
        "Not nil"
      end
    end
    ...
  end
end
Run Code Online (Sandbox Code Playgroud)

但这无助于理解 Ruby 的核心。非常感谢!

ruby metaprogramming

1
推荐指数
1
解决办法
608
查看次数

在 ruby​​ 中将 &:method 和 :method 作为函数参数传递的区别

我正在努力理解何时使用&符号将符号传递给表示方法的函数。例如,如果我想计算范围 1..10 的总和,我可以执行以下操作:

(1..10).inject(:+)

这最初让我相信,如果你想传递一个符号来定义一个方法来“神奇地”在函数中使用,你可以将函数名称作为符号传递。但是后来我在 Rails 中看到了类似的东西:

total = Product.find(product_list).sum(&:price)

如果我理解正确,&:price 与调用 :price.to_proc 相同。我不明白上面是如何工作的。

ruby symbols function-pointers metaprogramming ruby-on-rails

1
推荐指数
1
解决办法
1851
查看次数

T::* 在模板的参数中是什么意思?

按照这里写的文章:

我遇到了这个代码(为了清楚起见,缩短并更改了):

template <class T> struct hasSerialize
{
    // This helper struct permits us to check that serialize is truly a method.
    // The second argument must be of the type of the first.
    // For instance reallyHas<int, 10> would be substituted by reallyHas<int, int 10> and works!
    // reallyHas<int, &C::serialize> would be substituted by reallyHas<int, int &C::serialize> and fail!
    // Note: It only works with integral constants and pointers (so function pointers work).
    // In our case …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming template-meta-programming type-deduction

1
推荐指数
1
解决办法
749
查看次数

动态生成django表单

我正在尝试动态生成 python 类。在这里,一个 django 形式。

class BankForm(forms.Form):
    name = forms.CharField()

for i in range(10):
    setattr(BankForm, 'contact_' + str(i), forms.CharField())
Run Code Online (Sandbox Code Playgroud)

但是在使用表单时,只name显示该字段。关于如何做到这一点的任何建议?

编辑:发现了modelform_factory,看起来是一种解决方案

EDIT2:更好,看起来add_fields我可以使用一种方法

python django metaprogramming django-forms

1
推荐指数
1
解决办法
1538
查看次数

C++ 元函数(例如 std::conditional)的评估策略(eager、lazy、...)是什么?

C++14 草案 n4140 读取

T 应为枚举类型

template <class T> struct underlying_type

写作有多糟糕

std::conditional_t<std::is_enum<T>::value, std::underlying_type_t<T>, foo>
Run Code Online (Sandbox Code Playgroud)

什么时候T可以是任意类型?我会进入 UB 吗,编译器会删除我的 $HOME (因为语言律师说“在 UB 下任何事情都可能发生”)?

c++ metaprogramming operator-precedence template-meta-programming evaluation-strategy

1
推荐指数
1
解决办法
464
查看次数

无法为此类型派生Generic?

在GHC 8.6.2上编译这个简短的片段:

{-# LANGUAGE DeriveGeneric, PolyKinds #-}
import GHC.Generics

data Foo f
    = FA
    | FB (f (Foo f))
    deriving (Generic, Generic1)
Run Code Online (Sandbox Code Playgroud)

结果出现此错误:

Can't make a derived instance of ‘Generic1 Foo’:
  Constructor ‘FB’ applies a type to an argument involving the last parameter
                   but the applied type is not of kind * -> *
Run Code Online (Sandbox Code Playgroud)

是不是可以推导出Generic这样的类型?为什么?

haskell metaprogramming typeclass deriving

1
推荐指数
1
解决办法
145
查看次数

static_assert一个类型在std :: variant的可接受类型之中

在C ++ 17中,如何验证constexpr类型是否属于变量的类型列表?

例如:

using MyVt = std::variant<int, float>;
static_assert( MyVt::has_type< bool >::value, "oops, forgot bool");
Run Code Online (Sandbox Code Playgroud)

要么

static_assert( mpl::has_key< MyVt::typelist, T >::value, "oops, forgot T");
Run Code Online (Sandbox Code Playgroud)

当然,在概念表达式或static_assert模板函数中更有用。限制接受的可能类型。

如果我们不能为此使用显式支持的标准元功能或金属主义者,则可以使用涉及构造函数表达式的SFINAE破解检查吗?

c++ metaprogramming variant c++17

1
推荐指数
1
解决办法
348
查看次数

当内部有嵌套函数时,如何在R中以编程方式创建函数?

我的目标是使用代码创建以下功能:

s <- c(x = 10)
a <- c(i = 3)

model <- function(s, a) {
  with(as.list(c(s, a)), {
    y <- x * i
    y * 10
  })
}

model(s, a)
Run Code Online (Sandbox Code Playgroud)

结果应为300。

我正在解析另一个软件,可以从该软件中提取方程式作为字符串。因此,我需要从这些字符串构造函数的主体。

我一直在尝试使用rlang库无济于事。

library(rlang)

func_body <- "with(as.list(c(s, a)), {
  y <- x * i
  y * 10
})";

foo <- new_function(
  exprs(s =, a = ),
  expr(!!parse(text = func_body))
)
Run Code Online (Sandbox Code Playgroud)

任何的想法?

r metaprogramming rlang

1
推荐指数
1
解决办法
70
查看次数

模板功能使用参数包时如何传递其他模板参数?

函数/类使用参数包时是否可以具有其他模板参数?

我尝试简单的求和函数。我想使用template Printer课程打印一些东西。怎么做?如何告诉编译器专门“标记”第一个参数...或有一些解决方法。

下面的代码生成错误。

#include <iostream>

template <typename T>
double sum(T t)
{
    return t;
}

template <typename Printer, typename T, typename... Rest>
double sum(Printer printer, T t, Rest... rest)
{
    printer.print();

    return t + sum(rest...);
}

struct P
{
    void print() { std::cout << "= " << std::endl; }
};

int main()
{
    P printer;
    std::cout << sum(printer, 2, 3, 4.1) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming variadic-templates c++14

1
推荐指数
1
解决办法
54
查看次数