什么是;加上;D2.0中的关键字do?
在D2.0中有一个带有关键字的什么是;加上;D2.0中的关键字do?,d,D,在D2.0中有一个带有关键字的,但我不确定它是做什么的,或者如何使用它。我对文件的搜索没有结果。有人知道with关键字的用法吗?(是像C#的使用语句,还是像Visual Basic的使用子句?就在这里: with语句是简化对同一对象的重复引用的一种方法 struct Values { double x,y,vx,vy,ax,ay,dt; int i; void set_i( int i ) { this.i = i; } ali
,但我不确定它是做什么的,或者如何使用它。我对文件的搜索没有结果。有人知道with
关键字的用法吗?(是像C#的使用
语句,还是像Visual Basic的使用
子句?就在这里:
with语句是简化对同一对象的重复引用的一种方法
struct Values
{
double x,y,vx,vy,ax,ay,dt;
int i;
void set_i( int i )
{
this.i = i;
}
alias int NestedType;
};
void main()
{
//Setup:
Values vals;
vals.i = 10;
//Usage of with:
with(vals)
// with(otherVals) // <<-- Easy to switch the 'with' scope to deal with a different instance:
{
// Imports all the member symbols of the struct or class into present scope
// without needing to refer every time to vals:
assert( i == 10 );
// Good for "repeated references to the same object": Helpful for maths where the syntax needs to look familiar, and repeatedly accesses the same object:
x += vx*dt;
y += vy*dt;
vx += ax*dt;
vy += ay*dt;
// Call the member functions too:
set_i(42);
// ... and get nested types/enums etc etc
NestedType ex;
}
// Results are persist (i.e.: all above writes were by reference:
assert( vals.i == 42 );
//Equivalent usage without 'with':
{
Values* vp = &vals; // get a reference to vals
assert( vp.i == 42 );
// This looks a lot Uglier than the previous one:
vp.x += vp.vx*vp.dt;
vp.y += vp.vy*vp.dt;
vp.vx += vp.ax*vp.dt;
vp.vy += vp.ay*vp.dt;
vp.set_i(56);
Values.NestedType ex;
}
assert( vals.i == 56 );
}
它是这样使用的:
with (expression)
{
usage();
writef("%s, %s", access, member);
}
如果需要,可以使用with语句构造匿名对象:
class Foo { int x; }
void main()
{
with (new Foo)
{
x = 5;
}
}
有一些使用闪烁体的关键字的示例代码。来自文档:
“with语句可以简化对同一对象的重复引用。”
当我看到文档中提到它时,我的想法是,它将有助于数学,因为语法需要看起来很熟悉,代码会重复访问同一个对象
struct Values
{
double x,y,vx,vy,ax,ay,dt;
int i;
void set_i( int i )
{
this.i = i;
}
alias int NestedType;
};
void main()
{
//Setup:
Values vals;
vals.i = 10;
//Usage of with:
with(vals)
// with(otherVals) // <<-- Easy to switch the 'with' scope to deal with a different instance:
{
// Imports all the member symbols of the struct or class into present scope
// without needing to refer every time to vals:
assert( i == 10 );
// Good for "repeated references to the same object": Helpful for maths where the syntax needs to look familiar, and repeatedly accesses the same object:
x += vx*dt;
y += vy*dt;
vx += ax*dt;
vy += ay*dt;
// Call the member functions too:
set_i(42);
// ... and get nested types/enums etc etc
NestedType ex;
}
// Results are persist (i.e.: all above writes were by reference:
assert( vals.i == 42 );
//Equivalent usage without 'with':
{
Values* vp = &vals; // get a reference to vals
assert( vp.i == 42 );
// This looks a lot Uglier than the previous one:
vp.x += vp.vx*vp.dt;
vp.y += vp.vy*vp.dt;
vp.vx += vp.ax*vp.dt;
vp.vy += vp.ay*vp.dt;
vp.set_i(56);
Values.NestedType ex;
}
assert( vals.i == 56 );
}
struct值
{
双x,y,vx,vy,ax,ay,dt;
int i;
无效集_i(int i)
{
这个。i=i;
}
别名int NestedType;
};
void main()
{
//设置:
VAL值;
VAL.i=10;
//在以下情况下使用:
带(VAL)
//与(其他)//这是一个语句?我在“表达式”部分中查找,但找不到它…从搜索框中搜索显然不容易。谢谢!!:)你的答案信息量会更大,它还回答了问题,而不仅仅是链接到答案。D的with
语句类似于VB的with
条款