- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用一堆字段实现一个通用结构,其中每个字段类型都应该知道整个结构的确切类型。这是一种策略模式。
pub struct Example<S: Strategy<Example<S, D>>, D> {
pub s: S,
pub a: S::Associated,
pub data: D,
}
pub trait Strategy<T> {
type Associated;
fn run(&self, &T);
}
pub trait HasData {
type Data;
fn data(&self) -> &Self::Data;
}
impl<S: Strategy<Self>, D> Example<S, D> {
// ^^^^
// the complex code in this impl is the actual meat of the library:
pub fn do_it(&self) {
self.s.run(self); // using the Strategy trait
}
}
impl<S: Strategy<Self>, D> HasData for Example<S, D> {
type Data = D;
fn data(&self) -> &D {
&self.data
}
}
然后我打算从上面的“库”中实例化泛型:
pub struct ExampleStrat;
pub struct ExampleData;
impl<E: HasData<Data = ExampleData>> Strategy<E> for ExampleStrat {
type Associated = ();
fn run(&self, e: &E) {
let _ = e.data();
// uses ExampleData here
}
}
let example = Example {
s: ExampleStrat,
a: (),
data: ExampleData,
};
example.do_it();
在我的实际代码中,我有很多不同的“策略”和多个数据字段,因此 Example
type 有一个令人印象深刻的泛型列表,如果库用户不需要明确说明它们(或者至少不经常)而可以只使用 HasData
,我会很高兴。特征(及其关联类型,而不是泛型类型参数)。
如果 struct Example<S, D>
中没有类型绑定(bind),这实际上(令人惊讶地)很好,比我最初预期的要好得多(在 fighting with Self
in the struct
bounds 之后)。不过还是推荐给duplicate the impl
trait bounds on the struct
当结构只应该与受限类型一起使用时,在我的情况下,我实际上需要它们才能使用 Associated
输入 a
字段。
现在编译器报错了
error[E0275]: overflow evaluating the requirement `main::ExampleStrat: Strategy<Example<main::ExampleStrat, main::ExampleData>>`
--> src/main.rs:42:9
|
42 | a: (),
| ^^^^^
|
= note: required because of the requirements on the impl of `HasData` for `Example<main::ExampleStrat, main::ExampleData>`
= note: required because of the requirements on the impl of `Strategy<Example<main::ExampleStrat, main::ExampleData>>` for `main::ExampleStrat`
我该如何解决这个问题?我是在尝试做一些不可能的事情,我是不是做错了,或者这应该是可能的,但我正在成为 a compiler bug 的牺牲品。 ?我的完整设计有缺陷吗?
最佳答案
首先,如果您避免在结构和特征的定义上设置特征界限,一切都会变得更加清晰。当事情变得复杂时,约束至少从同一个方向解决。
pub struct Example<S, D, A> {
pub s: S,
pub a: A,
pub data: D,
}
pub trait Strategy<T> {
type Associated;
fn run(&self, &T);
}
pub trait HasData {
type Data;
fn data(&self) -> &Self::Data;
}
impl<S, D, A> Example<S, D, A>
where
S: Strategy<Self, Associated = A>,
{
pub fn do_it(&self) {
self.s.run(self);
}
}
impl<S, D, A> HasData for Example<S, D, A>
where
S: Strategy<Self, Associated = A>,
{
type Data = D;
fn data(&self) -> &D {
&self.data
}
}
ExampleStrat
的Strategy
实现如下所示:
impl<E: HasData<Data = ExampleData>> Strategy<E> for ExampleStrat {
type Associated = ();
// ...
}
这意味着您正在为所有可能的限定类型 E
定义它。类型检查器现在只能查看 trait bounds,它们又是通用的,并且只能根据其他 traits 来表示,这些 traits 将彼此用作边界,因此类型检查器进入了一个循环。通过给它一个你知道的具体类型,在循环中放置一个 block 。
pub struct ExampleStrat;
pub struct ExampleData;
impl Strategy<Example<ExampleStrat, ExampleData, ()>> for ExampleStrat {
type Associated = ();
fn run(&self, e: &Example<ExampleStrat, ExampleData, ()>) {
let _ = e.data();
// uses ExampleData here
}
}
fn main() {
let example = Example {
s: ExampleStrat,
a: (),
data: ExampleData,
};
example.do_it();
}
关于compiler-errors - 奇怪地重复出现的通用特征模式 : overflow evaluating the requirement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50657585/
我被卡住了,请重新审视这个问题。 我正在与其他人的意大利面条式代码一起工作,这些代码不再存在,并且有很多时间弄清楚他们正在评估什么。 在查询转储中,我看到
真实世界Haskell的第8章 globToRegex' (c:cs) = escape c ++ globToRegex' cs 这个函数不是尾递归的,它说答案依赖于 Haskell 非严格(惰性)
来自 Haskell,我正在阅读 Idris 关于懒惰(非严格)的故事。我翻了翻最近的发行说明,还有 found code类似于以下 myIf : (b : Bool) -> (t : Lazy a)
我正在读这个帖子 A custom find function并指出了这一点 有趣的结果。请注意,Evaluate 与 Application.Evaluate 相同,并且所需时间大约是 Active
R包裹mice带有以下示例: library("mice") imp <- mice(nhanes) fit <- with(data=imp,exp=lm(bmi~hyp+chl)) 我想要一个灵活
我正在尝试使用 PHPUnit 3.6.4 对我的 Zend Framework 应用程序进行单元测试。当我在命令提示符中尝试此命令时,出现以下错误。 C:\xampp\htdocs\testsamp
我希望函数的结果是: 所有值的计算结果均为 False(无、0、空字符串)-> True 所有值的计算结果为 True -> True 所有其他情况 -> 错误 这是我的尝试: >>> def con
我可以使用 puppeteer 导航到一个页面但后来page.evaluate没有返回任何响应。此外,我无法在 page.evaluate 内部进行调试。任何一个。我在 Debug模式下运行脚本( n
from surprise import Reader, Dataset, SVD from surprise import evaluate ----------------------------
使用 PhantomJS 考虑以下代码片段: var reloadAfterLogin = false; function(user, pass, debug){ // step 3 submit L
我知道流是惰性的,直到在流上调用终端方法。我所知道的是,在调用终端方法之后,所有中间方法都按调用顺序执行。 但是对于下面的程序,我无法理解流是如何工作的。这是代码,我试过了。 import java.
最近我开始研究 VS2012。 当我调试代码(这是 SharePoint 2013 应用程序)并尝试获取变量的值时 - 我一次又一次收到消息: Function evaluation disabled
我正在阅读 David Flanagan 的《JavaScript:权威指南》,这可能是世界上最厚的 JavaScript 书。在简要描述数组初始值设定项时,弗拉纳根说“每次数组初始值设定项时都会评估
在 puppeteer 截取屏幕截图之前,我试图让代码执行等待所有图像加载完毕。当调用 initData() 函数时,我的 DOM 被填充,该函数在客户端 js 文件中定义。延迟或超时是一种选择,但我
对国际通用标准有一些批评,如 [Under-attack]。 1 您认为使用 CC 开发 IT 产品的利弊是什么? 最佳答案 我是 BSI(德国)和 NIAPP(美国)计划的通用标准评估员。我有一些经
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 11 年前关闭。 Improve this
在非严格求值语言中,使用按名称调用与通过宏扩展调用有何区别和优点/缺点? 您能否提供一个示例来解释这两种评估策略? 谢谢! 最佳答案 按姓名调用: 按名称调用是一种求值策略,其中函数的参数在调用函数之
我真的无法在 Modelica 规范中找到任何答案,所以我想问问你们。规范指出如果表达式的值不影响结果(例如, bool 表达式的短路评估),工具可以自由地求解方程、重新排序表达式和不计算表达式。 I
我想尝试惰性表达式评估,但我现在不想深入研究 Haskel。拜托,你能帮忙找出其他语言有这个功能吗? 最佳答案 你可以用多种语言模拟它。 this例如,是 C++ 的通用惰性求值器。正如文章所说,它也
关注,据说foldl'是 foldl 的严格版本. 但是我很难理解,strict 是什么意思?意思是?? foldl f z0 xs0 = lgo z0 xs0 where
我是一名优秀的程序员,十分优秀!