我本来希望这会起作用:
user_ids = [1, 154, 31908]
query = "SELECT id FROM users WHERE id = ANY (ARRAY[$1])"
ActiveRecord::Base.connection.exec_query(query, "SQL", [[nil, user_ids]])
Run Code Online (Sandbox Code Playgroud)
然而这会导致异常TypeError: can't cast Array。
Rails似乎可以在使用某些模型的方法时处理数组参数where,但是在进行原始 SQL 查询时有没有办法拥有数组参数,而不涉及 ActiveRecord(除了获取连接)或任何模型?
考虑我定义了一个协议Frobbable。此外,我有一个有效的协议实现,以及一个缺少该.frob()方法的损坏的实现:
from typing import Protocol
from abc import abstractmethod
class Frobbable(Protocol):
@abstractmethod
def frob(self) -> None:
raise NotImplementedError
def main(knob: Frobbable) -> None:
knob.frob()
class Knob:
def frob(self) -> None:
print("knob has been frobbed")
class BrokenKnob:
pass
main(Knob())
main(BrokenKnob())
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,使用 mypy 检查该程序会导致错误:
testprotocol.py:25:6: error: Argument 1 to "main" has incompatible type "BrokenKnob"; expected "Frobbable" [arg-type]
main(BrokenKnob())
^
Found 1 error in 1 file (checked 1 source file)
Run Code Online (Sandbox Code Playgroud)
不幸的是,它没有提供任何关于为什么 BrokenKnob不兼容的信息:在这种情况下,它丢失了.frob()。如果没有这样的信息,在一个不平凡的程序(具有许多方法和许多实现的协议)中纠正这样的问题将变得非常乏味的苦差事。
有没有办法在不修改程序的情况下从 …
我正在尝试创建一个将函数指针作为参数的异步函数。它会做一些事情,调用函数,等待结果,然后再做一些事情:
use std::future::Future;
async fn run_another_async_fn<F, Fut>(f: F)
where
Fut: Future<Output = ()>,
F: FnOnce(&mut i32) -> Fut,
{
let mut i = 42;
println!("running function");
f(&mut i).await;
println!("ran function");
}
async fn foo(i: &mut i32) {}
async fn bar() {
run_another_async_fn(foo);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这无法编译:
error[E0308]: mismatched types
--> src/lib.rs:17:5
|
17 | run_another_async_fn(foo);
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected associated type `<for<'_> fn(&mut i32) -> impl Future {foo} as FnOnce<(&mut i32,)>>::Output`
found …Run Code Online (Sandbox Code Playgroud) 我有一个带有自动流程的存储库,该流程创建了很多标签。例:
* 5391e27 - (HEAD -> master, origin/master) Add a webhook to notify Phabricator of builds (2 hours ago) <Phil Frost>
* c380a48 - Retry downloading Selenium 3 times (2 hours ago) <Phil Frost>
| * 542731c - (tag: phabricator/diff/962) Retry downloading Selenium 3 times (6 hours ago) <Phil Frost>
|/
* 59509a3 - (tag: phabricator/base/962) Notify only on "master" branch (7 hours ago) <Phil Frost>
* 1504aa6 - Fix a few errors and omissions in README (7 hours ago) …Run Code Online (Sandbox Code Playgroud) 我有一个使用 autotools 作为构建系统的 C++ 项目。
我的程序涉及一个抽象接口,其中有多个实现,每个实现都在单独的源文件中。这些实现取决于可能安装也可能不安装的库,具体取决于用户偏好和平台。
我想配置自动工具来跳过构建缺乏底层依赖项的实现。我想以用户和包维护者通常期望的任何方式来这样做。
举个例子,假设我的程序输出音频,它可以使用OSS、ALSA或JACK来实现。oss_output.cc这些分别在、alsa_output.cc和中实现jack_output.cc。
目前我的Makefile.am包含(以及其他一些内容):
myprog_SOURCES = \
src/oss_output.cc \
src/alsa_output.cc \
src/jack_output.cc
myprog_LDFLAGS = [...some other stuff...] -ljack
Run Code Online (Sandbox Code Playgroud)
不幸的是,尝试在未安装 JACK 的系统上构建此程序会失败,因为链接器找不到 libjack 和/或编译器因缺少 JACK 的头文件而失败。如果未安装 JACK,我宁愿src/jack_output.cc从中删除myprog_SOURCES,然后-ljack从 中删除myprog_LDFLAGS。ALSA 和 OSS 也类似。
在这些依赖项(OSS、ALSA 和 JACK)中,其中一些库使用pkg-config. 有些没有,但我仍然可以通过头文件和库的存在来检测它们的存在。
如何使用自动工具来实现这一点,并注意遵循最佳实践和惯例?