- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究 lambda 演算,但由于我不太懂泛型,所以我想确认我对某些事情的理解并解决一个错误。考虑以下定义:
pub trait Term {} // a lambda term - a variable, an abstraction or an application
pub struct Var(pub usize); // a variable, De Bruijn style
pub struct Abs<T: Term>(T); // an abstraction
pub struct App<T: Term, U: Term>(T, U); // application of T on U
我明白(即,否则它不起作用)我需要 App
在 <T: Term, U: Term>
上通用而不仅仅是 <T: Term>
能够例如申请Var
到App
,即有一个 App(Var(x), App(...))
.
上述结构都是Term
小号:
impl Term for Var {}
impl<T: Term> Term for Abs<T> {}
impl<T: Term> Term for App<T, T> {}
有趣的是我不需要 App<T, U>
在这里,但希望到目前为止一切顺利 - 现在我想实现 fmt::Display
对于上述结构:
use std::fmt;
use std::fmt::Display;
impl fmt::Display for Var {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T: Term+Display> Display for Abs<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "λ{}", self.0)
}
}
这两个impl
工作得很好;我将它们包括在内,因为下一个依赖于它们:
impl<T: Term+Display> Display for App<T, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
App(Var(_), Var(_)) => write!(f, "{}{}", self.0, self.1),
_ => unimplemented!()
}
}
}
但它失败了:
error[E0308]: mismatched types
--> src\ast.rs:34:8
|
34 | App(Var(_), Var(_)) => write!(f, "{}{}", self.0, self.1),
| ^^^^^^ expected type parameter, found struct `ast::Var`
|
= note: expected type `T`
= note: found type `ast::Var`
我想打印 App
根据其内容的类型不同。我试图找到一个相关的问题,但它们主要围绕关联类型展开。是否有简单的解决方案,还是我必须重新定义定义?
最佳答案
I would like to print
App
differently based on the types of its contents.
这很简单,只需实现 Display
对于每种独特类型的 App
您希望能够打印:
use std::fmt;
struct App<T, U>(T, U);
impl fmt::Display for App<i32, bool> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Awesome choice!")
}
}
impl fmt::Display for App<bool, String> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Woah, a string? Really?")
}
}
fn main() {
println!("{}", App(42i32, false));
println!("{}", App(true, "wow".to_string()));
}
您还可以接受具有自己泛型的其他类型:
impl<T> fmt::Display for App<Vec<T>, char> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} of them; {}", self.0.len(), self.1)
}
}
请注意,这没有 unimplemented!
称呼;一切都在编译时检查:
error[E0277]: the trait bound `App<bool, bool>: std::fmt::Display` is not satisfied
--> src/main.rs:24:24
|
24 | println!("{}", App(true, false));
| ^^^^^^^^^^^^^^^^ the trait `std::fmt::Display` is not implemented for `App<bool, bool>`
|
= note: `App<bool, bool>` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string
= note: required by `std::fmt::Display::fmt`
这在标准库中有一个相似的类比: Cursor
.此类型接受泛型,但所有有趣的功能仅针对少数具体类型实现,如 &[u8]
或 Vec<u8>
.
but it still causes trouble, e.g. with
App<Var, App>
, because the innerApp
expects 2 type arguments again
是的,你必须指定泛型,因为 App
不是一种类型,它只是通向一个类型的垫脚石。
这完全取决于你想做什么。最简单的就是有一个 App
由两种任意类型组成,只要你不使用它们:
impl<T, U> fmt::Display for App<i32, App<T, U>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Semi-awesome")
}
}
如果你希望能够显示App
, 那么你需要限制泛型使得 App
可显示:
impl<T, U> fmt::Display for App<i32, App<T, U>>
where App<T, U>: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}) squared!", self.1)
}
}
检查一下
fn main() {
let a = App(42i32, false);
println!("{}", a);
let b = App(100, a);
println!("{}", b);
let c = App(100, b);
println!("{}", c);
}
我猜接下来的问题将是关于对所有非特殊条件进行某种回退或默认情况的问题。像这样的东西:
impl fmt::Display for App<i32, bool> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Awesome choice!")
}
}
impl<T: fmt::Display, U: fmt::Display> fmt::Display for App<T, U> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Boring: {} + {}", self.0, self.1)
}
}
不,那不会编译! i32
和 bool
同时实现Display
所以选择哪个实现是不明确的。此时,您进入了 specialization 的领域.这迫使您真正理解孤儿规则。
据我了解当前的特化实现,您不能专注于具体类型,只能专注于特征。
关于generics - 如何匹配具体类型的通用结构字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42794578/
我是 Robert,我在使用 JavaScript 时遇到了一些问题。 我得到了一个 (这是隐藏的)。我唯一想问你的是:我想检查日期是否在 中已通过。如果通过了我想改变CSS中容器的背景颜色。不幸的
所以我的问题是我想要求输入使用扫描仪的信息,但它根本不打印出来。当它显示跳过的扫描仪的值时,Scanner CheeseType = new Scanner(System.in);,我得到 null。
Fe_Order_Items fe_order_items_id fe_order_specification_id fe_users_id fe_menu_items_id fe_order_ite
人们普遍提到 - “Celery 是一个基于分布式消息传递的异步任务队列/作业队列”。虽然我知道如何使用 Celery 工作人员等。但内心深处我不明白分布式消息传递的真正重要性和意义以及任务队列在其中
我试图理解下面的代码,但有一些我以前从未见过的东西,那就是:“\&\&” 这是代码: int main() { fork() \&\& (fork() || fork()); exit(EXIT_SU
您好,我是论坛新手。 我有很多使用 python 的经验,但没有使用 tkinter 的经验。 这是我的代码: from tkinter import * def Done(): celEn
在 C# 中,假设我们有一个通用类和一个具体类 [Serializable] public class GenericUser { ... [Serializable] public class Co
我尝试使用的库有一个通用抽象类,其中有两个实现该基础的子类。我想编写一个类,它将根据构造函数参数的参数类型自动创建其中一个子级的实例。 基类没有默认构造函数 基类的构造函数也需要其他通用类的实例 代码
我是 Angular 的新手,我一直在尝试了解它的工作原理。我正在制作一个简单的应用程序,其中有人可以通过简单的 html 界面添加用户并使用 SQLite 将其存储在数据库中,然后他们可以编辑或删除
我想创建一个用于存储数据的对象,限制读/写访问。 例如: OBJ obj1; OBJ obj2; // DataOBJ has 2 methods : read() and write() DataO
注入(inject)/隔离密封在 dll 中且不实现接口(interface)的类的首选方法是什么? 我们使用 Ninject。 假设我们有一个类“Server”,我们想要注入(inject)/隔离“
在花费了至少 10 个小时的时间浏览在线资源、视频和教程之后,我有两个关于将我的 Android 应用程序与 mySQL 数据库连接的问题。 保存文件 1) 所有教程都将 php 文件保存在 C/WA
许多有经验的开发人员建议不要使用 Django multi-table inheritance因为它的性能不佳: Django gotcha: concrete inheritance通过 Jacob
我知道我冒着挨揍的风险,但我觉得我在这件事上要绕圈子。为了让模型可用于多个项目,我们已将模型移出到一个单独的项目(一个 DLL)中,作为一系列要实现的接口(interface)。我们的界面上有这一行:
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我遇到了一个特定 mac 的问题,它没有显示我正确构建的某个网站。我测试过的所有其他 mac 和 pc 都能正确显示网站,但是在所有浏览器中这个特定的 mac 显示不正确就像提到的那样,这在其他每台计
给定这段代码 public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFold
我已经搜索了网络和我的服务器,但我无法找到我网站的 php.ini。我的网站出现以下错误。 Class 'finfo' not found Details G:\inetpub\wwwroot\lan
SQL 爱好者: 我正在尝试通过玩以下用例来挖掘我一些生疏的 sql 技能: 假设我们有一家有线电视公司,并且有跟踪的数据库表: 电视节目, 观看我们节目的客户,以及 观看事件(特定客户观看特定节目的
我正在设计一个使用 HTML5 网络组件(HTML 导入、影子 DOM、模板和自定义 HTML 元素)的网络应用程序,这些组件是通过普通 JavaScript(无框架)实现的。 Web 应用程序相当简
我是一名优秀的程序员,十分优秀!