语言提案 [英] Language proposal
问题描述
如果我想验证方法的返回值,但是这个方法有一个返回语句的几个点,那么该怎么办?
我知道在MSIL中只有每种方法都有一个RET操作码,这意味着该方法总是知道它的返回值。
int GetValue()
{
试试
{
//计算价值,几次返回'在这里
如果(a< b)返回b;
否则如果(a == 1)返回b + 1;
else if(b == 1)rerturn a-1;
否则返回2;
}
终于
{
System.Diagnostics .Debug.Assert(RETVAL!= 0); //新关键字retval
}
}
我知道我也可以使用变量存储返回值但是
意味着要引入一个新变量。
在Pascal中你可以使用函数名来访问返回值。
什么你觉得呢?
-
cody
免费工具,游戏和幽默
< a rel =nofollowhref =http://www.deutronium.de.vutarget =_ blank> http://www.deutronium.de.vu || http://www.deutronium.tk
对我来说似乎很特别。看看AspectJ或Eiffel是否真的可以解决这个问题。
Niki
"科迪" <无**************** @ gmx.net>写在
新闻:ub ************** @ TK2MSFTNGP09.phx.gbl ...如果我想验证怎么办?一个方法的返回值,但是这个方法
有几个返回语句的点。
我知道在MSIL中每个方法只有一个RET操作码,这意味着该方法总是如此此时知道它的返回值。
int GetValue()
{
尝试
{
//计算值,几个返回'在这里
if(a< b)返回b;
else if(a == 1)return b + 1;
else if(b == 1)rerturn a-1;
>否则返回2;
}
最后
{
System.Diagnostics.Debug.Assert(retval!= 0); //新关键字retval
}
我知道我也可以使用变量存储返回值但是
意味着要引入一个新变量。
在Pascal中你可以使用函数名来访问返回值。
你怎么看?
-
cody
免费软件工具,游戏和幽默
http://www.deutronium.de .vu || http://www.deutronium.tk
>
>对我来说似乎很特别。看看AspectJ或Eiffel是否真的该问题的通用解决方案。
我知道有些语言更关心验证代码
C#的确如此。这就是我提出它的原因。
-
cody
免费软件工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk
>
"科迪" <无**************** @ gmx.net>写道:
如果我想验证一个方法的返回值怎么办?
这个方法有几个返回
语句的点。
[...]
我知道我也可以使用一个变量来存储
返回值但这意味着要引入一个新的变量。
一个额外的变量不会杀了你,并且可能有助于提高可读性。
试图遵循程序的执行路径可能很尴尬
当有多个退货单时。
P.
What if i want to validate the return value of a method but this method has
serveral points where a return statement is.
I know in MSIL there is only one RET opcode in each method and this means
that the method always knows its return value at this point.
int GetValue()
{
try
{
// calculate value, severals return''s here
if (a<b)return b;
else if (a==1) return b+1;
else if (b==1) rerturn a-1;
else return 2;
}
finally
{
System.Diagnostics.Debug.Assert(retval!=0); // new keyword retval
}
}
I know I also could use a variable where I store the return value but that
would mean to introduce a new variable.
In Pascal you could access the return value using the function''s name.
What do you think?
--
cody
Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Seems to much ad-hoc for me. Have a look at AspectJ or Eiffel for really
generic solutions to that problem.
Niki
"cody" <no****************@gmx.net> wrote in
news:ub**************@TK2MSFTNGP09.phx.gbl...What if i want to validate the return value of a method but this method has serveral points where a return statement is.
I know in MSIL there is only one RET opcode in each method and this means
that the method always knows its return value at this point.
int GetValue()
{
try
{
// calculate value, severals return''s here
if (a<b)return b;
else if (a==1) return b+1;
else if (b==1) rerturn a-1;
else return 2;
}
finally
{
System.Diagnostics.Debug.Assert(retval!=0); // new keyword retval
}
}
I know I also could use a variable where I store the return value but that
would mean to introduce a new variable.
In Pascal you could access the return value using the function''s name.
What do you think?
--
cody
Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
> Seems to much ad-hoc for me. Have a look at AspectJ or Eiffel for reallygeneric solutions to that problem.
I know that there are languages that care much more about validation code as
C# does. Thats exactly why I was proposing it.
--
cody
Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"cody" <no****************@gmx.net> wrote:
What if i want to validate the return value of a method
but this method has serveral points where a return
statement is.
[...]
I know I also could use a variable where I store the
return value but that would mean to introduce a new
variable.
One extra variable won''t kill you, and will probably help readability.
Trying to follow the execution path of a program can be quite awkward
when there are multiple return statements.
P.
这篇关于语言提案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!