- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有兴趣在功能上类似于 Rust 中的关键字参数,目前不支持它们。
对于提供关键字参数的语言,类似这样的事情很常见:
panel.button(label="Some Button")
panel.button(label="Test", align=Center, icon=CIRCLE)
我见过使用构建器模式处理的,例如:
ui::Button::new().label("Some Button").build(panel)
ui::Button::new().label("Test").align(Center).icon(CIRCLE).build(panel)
这很好,但与 Python 中的关键字参数相比有时有点尴尬。
但是使用 impl Default
的结构初始化和 Option<..>
Rust 中的成员可用于获得非常接近于实际上类似于编写关键字参数的东西,例如:
ui::button(ButtonArgs { label: "Some Button".to_string(), .. Default::default() } );
ui::button(ButtonArgs {
label: "Test".to_string(),
align: Some(Center),
icon: Some(Circle),
.. Default::default()
});
这可行,但在尝试用作关键字参数的上下文中有一些缺点:
struct
的名称Some(..)
围绕每个可选参数都很烦人/冗长。.. Default::default()
每次使用结束时都有点乏味。有没有办法减少其中的一些问题,(例如使用宏) 以替代关键字访问使这项工作更容易?
最佳答案
免责声明:我建议不要使用此解决方案,因为报告的错误非常可怕。从代码角度来看,最简洁的解决方案很可能是构建器模式。
除此之外...我拼凑了一个证明运算符(operator)滥用的概念验证。
与使用 struct 语法传递参数或使用构建器相比,它的主要优势在于它允许在采用不同的相同参数集的函数之间重用。
另一方面,它确实需要导入大量符号(要使用的每个名称)。
看起来像:
// Rust doesn't allow overloading `=`, so I picked `<<`.
fn main() {
let p = Panel;
p.button(LABEL << "Hello", ALIGNMENT << Alignment::Center);
p.button(LABEL << "Hello", Alignment::Left);
p.button(Label::new("Hello"), Alignment::Left);
}
请注意,名称实际上是可选的,它只是作为参数本身的构建器,但如果您已经有了参数,则可以避开它。这也意味着可能不值得为“明显的”参数创建名称(此处为Alignment
)。
button
的正常定义:
#[derive(Debug)]
struct Label(&'static str);
#[derive(Debug)]
enum Alignment { Left, Center, Right }
struct Panel;
impl Panel {
fn button(&self, label: Label, align: Alignment) {
println!("{:?} {:?}", label, align)
}
}
需要一些增强:
impl Carrier for Label {
type Item = &'static str;
fn new(item: &'static str) -> Self { Label(item) }
}
impl Carrier for Alignment {
type Item = Alignment;
fn new(item: Alignment) -> Self { item }
}
const LABEL: &'static Argument<Label> = &Argument { _marker: PhantomData };
const ALIGNMENT: &'static Argument<Alignment> = &Argument { _marker: PhantomData };
是的,这确实意味着您可以扩充第 3 方库中定义的函数/方法。
这得到了支持:
trait Carrier {
type Item;
fn new(item: Self::Item) -> Self;
}
struct Argument<C: Carrier> {
_marker: PhantomData<*const C>,
}
impl<C: Carrier> Argument<C> {
fn create<I>(&self, item: I) -> C
where I: Into<<C as Carrier>::Item>
{
<C as Carrier>::new(item.into())
}
}
impl<R, C> std::ops::Shl<R> for &'static Argument<C>
where R: Into<<C as Carrier>::Item>,
C: Carrier
{
type Output = C;
fn shl(self, rhs: R) -> C {
self.create(rhs)
}
}
请注意,这不涉及:
如果用户有足够的耐心来枚举可选参数的所有组合,那么像@ljedrz 这样的解决方案是可能的:
struct ButtonArgs {
label: Label,
align: Alignment,
icon: Icon,
}
impl From<Label> for ButtonArgs {
fn from(t: Label) -> ButtonArgs {
ButtonArgs { label: t, align: Alignment::Center, icon: Icon::Circle }
}
}
impl From<(Label, Alignment)> for ButtonArgs {
fn from(t: (Label, Alignment)) -> ButtonArgs {
ButtonArgs { label: t.0, align: t.1, icon: Icon::Circle }
}
}
impl From<(Label, Icon)> for ButtonArgs {
fn from(t: (Label, Icon)) -> ButtonArgs {
ButtonArgs { label: t.0, align: Alignment::Center, icon: t.1 }
}
}
impl From<(Label, Alignment, Icon)> for ButtonArgs {
fn from(t: (Label, Alignment, Icon)) -> ButtonArgs {
ButtonArgs { label: t.0, align: t.1, icon: t.2 }
}
}
impl From<(Label, Icon, Alignment)> for ButtonArgs {
fn from(t: (Label, Icon, Alignment)) -> ButtonArgs {
ButtonArgs { label: t.0, align: t.2, icon: t.1 }
}
}
然后将允许以下所有组合:
fn main() {
let p = Panel;
p.button( LABEL << "Hello" );
p.button((LABEL << "Hello"));
p.button((LABEL << "Hello", ALIGNMENT << Alignment::Left));
p.button((LABEL << "Hello", ICON << Icon::Circle));
p.button((LABEL << "Hello", ALIGNMENT << Alignment::Left, ICON << Icon::Circle));
p.button((LABEL << "Hello", ICON << Icon::Circle, ALIGNMENT << Alignment::Left));
p.button(Label::new("Hello"));
p.button((LABEL << "Hello", Alignment::Left, Icon::Circle));
}
当有多个参数时,额外的一组括号是必要的。
但是有一个很大的缺点:使用错误的参数集会降低用户体验。
调用p.button("Hello");
的结果是:
error[E0277]: the trait bound `ButtonArgs: std::convert::From<&str>` is not satisfied --> <anon>:124:7
| 124 | p.button("Hello");
| ^^^^^^ the trait `std::convert::From<&str>` is not implemented for `ButtonArgs`
|
= help: the following implementations were found:
= help: <ButtonArgs as std::convert::From<Label>>
= help: <ButtonArgs as std::convert::From<(Label, Alignment)>>
= help: <ButtonArgs as std::convert::From<(Label, Icon)>>
= help: <ButtonArgs as std::convert::From<(Label, Alignment, Icon)>>
= help: and 1 others
= note: required because of the requirements on the impl of `std::convert::Into<ButtonArgs>` for `&str`
关于rust - 如何在 Rust 中最好地 *fake* 关键字样式函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41629819/
如果我创建一个对象时没有使用 new 关键字,例如“Object s(someval)”,但该对象的构造函数使用了 new,当该对象超出范围时,是否会调用析构函数为其分配新的空间?我感觉好像是,但我不
在 SQL 语法中,我发现奇怪的规则表明 select * from ONLY (t1)是有效的 SQL。 我的问题是:什么是 ONLY在这种情况下是什么意思? 它在规范的“7.6 table ref
为什么使用 $(this) 而不是重新选择类很重要? 我在代码中使用了大量的动画和 CSS 编辑,并且我知道可以使用 $(this) 来简化它。 最佳答案 当您通过 jQuery 执行 DOM 查询(
我正在尝试使用 IN 关键字编写查询。 表A 属性标识、属性名称 表B key 、属性标识、属性值 根据提供的 key ,我想返回所有 attrName、attrVal 组合。结果将包含两个表中的列。
这个问题在这里已经有了答案: Why would you use "AS" when aliasing a SQL table? (8 个答案) 关闭 9 年前。 我不擅长写查询,但是从我开始使用
我读过,在 Java 中,您不必将 this 关键字显式绑定(bind)到对象,它由解释器完成。它与 Javascript 相反,在 Javascript 中你总是必须知道 this 的值。但是 Ja
Swift 中“with”关键字的用途是什么?到目前为止,我发现如果您需要覆盖现有的全局函数,例如 toDebugString,可以使用该关键字。 // without "with" you
这个问题在这里已经有了答案: What does the keyword "where" in a class declaration do? (7 个答案) 关闭 9 年前。 在下面的一段代码中(
免责声明:swift 菜鸟 您好,我刚刚开始学习 Swift,正在学习 Swift 编程语言(Apple 在 WWDC 期间发布的书籍),并且想知道“where”关键字是什么。它用于 let vege
深入研究文档后,我找不到以下问题的答案: 是否有任何理由反对使用 this 来引用当前对象,如下例所示? type MyStruct struct { someField string } fun
前言 最近在做THINKPHP开发项目中,用到了 parent:: 关键字,实际上 parent::关键字 是PHP中常要用到的一个功能,这不仅仅是在 THINKPHP 项目开发中,即使是一个小型
我们都知道且经常用到 unsigned 关键字,但有没有想过,与此对应的 signed 关键字有啥用? 复制代码 代码如下: int i = 0; signed
this关键字再java里面是一个我认为非常不好理解的概念,:)也许是太笨的原因 this 关键字的含义:可为以调用了其方法的那个对象生成相应的句柄。 怎么理解这段话呢? thinking i
一 什么是 synchronized synchronized 关键字提供了一种锁机制,能够确保共享变量互斥访问,从而防止数据不一致问题的出现。 synchronized 关键字包括 monitor
最近看了几篇 synchronized 关键字的相关文章,收获很大,想着总结一下该关键字的相关内容。 1、synchronized 的作用 原子性:所谓原子性就是指一个操作或者多个操作,要么全部执行并
在本教程中,您将借助示例了解 JavaScript 对象方法和 this 关键字。 在 JavaScript 中,对象也可以包含函数。例如, // object containing meth
有人可以解释一下 PHP“with”的作用吗? 示例开始: 假设我有一个类: \App\fa_batch 这句话有什么区别: $w = (with (new \App\fa_batch))
这个问题在这里已经有了答案: What is the difference between using the colon and as syntax for declaring type? (2
如果我在 WHERE 子句中使用以下任一项,是否会有很大不同: WHERE [Process Code] = 1 AND ([Material ID] = 'PLT' OR [Material ID]
This question is unlikely to help any future visitors; it is only relevant to a small geographic are
我是一名优秀的程序员,十分优秀!