"与"在D语言中阻止

时间:2014-06-10 08:35:50

标签: d with-statement

D语言中是否有经典的with块?

我正在寻找以下行为:

struct Address {
  string street;
  int number;
}

struct Person {
  Address address;
}

// in some function
Person p = ...
with(p.address) {
  street = "Wide St.";
  number = 123;
}

// I am open to other solutions, if they could work e.g.
p.address.with => { street = ...; number = ...; }

assert(p.address.street == "Wide St.");

如果它不可用,您将如何更新嵌套结构的多个字段?在C ++中,我将嵌套分配给引用。

1 个答案:

答案 0 :(得分:15)

为什么不咨询D Language Reference?只需点击几下,我就会找到http://dlang.org/statement#WithStatement来回答您的问题。 如果你懒得关注链接,这里是简短的回答:是的,D有with statement,它的行为与你想要的完全一样