我是一位经验丰富的开发人员,通常使用 C# 和 Python 进行开发,做过大量元编程。老实说:我不喜欢 PHP,但最近的版本看起来至少比旧版本更有希望。我最近必须在 PHP 项目中工作,所以我必须提高我在这方面的技能。
我正在寻找资源以尽快了解 PHP 语言的详细信息。我当然能够阅读文档,而且我不是在寻找一些印刷的 API 文档。
我想了解这些语言背后的设计思想以及什么样的元编程是可能的。
任何提示?
所以我是提升 MPL 的新手,我不知道如何将它与标准类型一起使用。
我想要一个隐藏这种类型的元函数:
std::tuple<T0, T1, ..., TN>
Run Code Online (Sandbox Code Playgroud)
进入这个:
std::tuple<
std::function<T0(std::tuple<T0, T1, ...>, std::tuple<T0, T1, ...>)>,
std::function<T1(std::tuple<T0, T1, ...>, std::tuple<T0, T1, ...>)>,
...,
std::function<TN(...)>
>
Run Code Online (Sandbox Code Playgroud)
似乎这可以通过transform来完成,但我想要一个元组类型,而不是类型的向量。(它实际上不必使用 MPL,但我想它会更短?)
背景:目前我使用完全通用的类型,如果使用错误,则依赖所有地狱,但我想计算TupleOfFunctions以获得正确的错误。
template<class TupleOfValues, class TupleOfFunctions>
void f(TupleOfValues v, TupleOfFunctions fun)
Run Code Online (Sandbox Code Playgroud) AOP 和元编程之间有什么区别吗?我们能否说元编程技术(IL 编织、动态子类化等)是实现 AOP 的机制,它更多地是将横切关注点与关注实际业务需求的主要应用程序代码分离?
我可以通过它的名字来获取一个类吗?例如:
class Foo {
}
class Bar {
}
let x = "Foo"
classByString(x) // need to return Foo
Run Code Online (Sandbox Code Playgroud)
我想使用元编程来减少代码维护。
我试图动态创建一个函数,可以根据它在F#中的输入返回不同的类型.这些类型的函数类似于代理,以说明我想要做的事情,下面是一个不能正常工作的示例:
open FSharp.Reflection
open System
let functionThatReturnsAsync (irrelevantArgs: obj list) (returnType: Type) =
if returnType.GUID = typeof<string>.GUID
then async { return box "some text" }
elif returnType.GUID = typeof<int>.GUID
then async { return box 42 }
elif returnType.GUID = typeof<bool>.GUID
then async { return box true }
else async { return box null }
// this works fine
let func = FSharpValue.MakeFunction(typeof<string -> Async<int>>, fun x -> box (functionThatReturnsAsync [x] typeof<int>))
// unboxing to that type works as …Run Code Online (Sandbox Code Playgroud) 我想基于参数来评估导入模块的表达式expr.到目前为止,我想出了:
julia> expr = :(Base.Threads)
julia> @eval using $expr
ERROR: TypeError: import or using: expected Symbol, got Expr
Stacktrace:
[1] eval(::Module, ::Expr) at ./sysimg.jl:23
Run Code Online (Sandbox Code Playgroud)
一种可能性是Expr直接使用构造函数,如下所示:
julia> expr = [:Base, :Threads]
2-element Array{Symbol,1}:
:Base
:Threads
julia> eval(Expr(:using, expr...))
Run Code Online (Sandbox Code Playgroud)
但是有没有其他的,也许更直接的方式,而不需要构建Expr?
在我目前的项目中,我在某些类中有以下重复模式:
class MyClass
def method1(pars1, ...)
preamble
# implementation method1
rescue StandardError => e
recovery
end
def method2(pars2, ...)
preamble
# implementation method2
rescue StandardError => e
recovery
end
# more methods with the same pattern
end
Run Code Online (Sandbox Code Playgroud)
所以,我一直在考虑如何干燥这种重复模式.我的目标是拥有这样的东西:
class MyClass
define_operation :method1, pars1, ... do
# implementation method1
end
define_operation :method2, pars2, ... do
# implementation method2
end
# more methods with the same pattern but generated with define_wrapper_method
end
Run Code Online (Sandbox Code Playgroud)
我试图实现一种metagenerator,但我遇到了转发接收生成器的块的问题.这或多或少是我尝试过的:
def define_operation(op_name, *pars, &block)
define_method(op_name.to_s) do |*pars|
preamble
yield …Run Code Online (Sandbox Code Playgroud) 我syn用来解析Rust代码。当我使用读取命名字段的类型时field.ty,得到一个syn::Type。当我使用打印时,quote!{#ty}.to_string()我得到了"Option<String>"。
我怎样才能得到"String"?我想用#ty在quote!打印"String"代替"Option<String>"。
我想生成如下代码:
impl Foo {
pub set_bar(&mut self, v: String) {
self.bar = Some(v);
}
}
Run Code Online (Sandbox Code Playgroud)
从...开始
struct Foo {
bar: Option<String>
}
Run Code Online (Sandbox Code Playgroud)
我的尝试:
let ast: DeriveInput = parse_macro_input!(input as DeriveInput);
let data: Data = ast.data;
match data {
Data::Struct(ref data) => match data.fields {
Fields::Named(ref fields) => {
fields.named.iter().for_each(|field| {
let name = &field.ident.clone().unwrap();
let ty = &field.ty; …Run Code Online (Sandbox Code Playgroud) 我在将列表传递到宏时遇到问题,该列表将用于生成函数名称。例如,下面的代码将导致错误。
(defmacro gen (str-lst)
`(defun ,(intern (string-upcase (car str-lst))) () (print "foo")))
(gen '("foo" "bar"))
Run Code Online (Sandbox Code Playgroud)
产生的错误是:
***-DEFUN / DEFMACRO:QUOTE是一种特殊的运算符,不能重新定义。可以使用以下重新启动:ABORT:R1
中止主循环
我应该如何修改我的代码,我的代码有什么问题?
令我更加困惑的是,下面的代码(有关答案在此处退出)的运行正常。
(defmacro easy-one (str-lst)
`(mapc #'(lambda (str) (print str)) ,str-lst))
(easy-one '("foo" "bar"))
Run Code Online (Sandbox Code Playgroud) 朱莉娅手册指出:
每个Julia程序都以字符串开头:
Run Code Online (Sandbox Code Playgroud)julia> prog = "1 + 1" "1 + 1"
我可以很容易地获得简单表达式的AST,甚至可以在quote/ 的帮助下获得一个函数,如果我在字符串中包含表达式code_*,则可以使用Meta.parse/ Meta.show_sexpr。
问题:是否有办法获取编码的整个AST,可能包括几个原子表达式?像,读取源文件并将其转换为AST?