没有副作用的方法(提案) [英] methods without sideeffects (proposal)

查看:147
本文介绍了没有副作用的方法(提案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该有一种方法可以使用特殊的

属性标记没有副作用的方法,这样编译器就可以识别它们并且能够发出

警告:


公共类字符串

{

[NoSideEffect()] //或者我们可以命名它

[RequireUseReturnValue()]

public string Trim()

{

// ...

} <
}


这对于像string这样的不可变类特别有用。一个常见的

错误是你做的:


myString.TrimEnd('','','''');


并且作为效果,没有任何反应,因为我们忘记分配新的

创建的字符串。


-

cody


免费软件工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk

解决方案

是的,我能理解。我认为问题在于

方法的命名。方法名称''Trim''就像''Replace''一样,暗示它实际上是修改对象本身而不是返回修改过的对象。

也许如果他们用''获取'前缀这些方法,那就不会那么令人困惑。


虽然我喜欢用

属性,我不认为''NoSideEffect''或''RequiresUseReturnValue''真的

解释得很好。


"科迪" <无**************** @ gmx.net>在留言中写道

news:u9 ************** @ tk2msftngp13.phx.gbl ...

应该有办法使用特殊的属性标记没有副作用的方法,以便编译器可以识别它们并且能够发出
警告:

公共类字符串
{
[NoSideEffect()] //或者我们可以命名它
[RequireUseReturnValue()]
公共字符串修剪()
{
// ...
这对于像string这样的不可变类特别有用。一个
的常见错误是你做的:

myString.TrimEnd('','','''')

并且作为一种效果,没什么因为我们忘了分配新的
创建的字符串。

- cody

免费工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk


>是的,我能理解。我认为问题在于

方法的命名。方法名称''Trim'',就像''Replace''一样,暗示它'
实际修改对象本身而不是返回修改后的对象。
也许如果他们为方法加上'前缀' '得''它不会让b $ b混乱。
虽然我喜欢用
属性描述方法行为的概念,但我不认为''NoSideEffect ''或''RequiresUseReturnValue''真的很好地解释它。



这个属性适用于编译器,如果方法
$ b将发出警告调用$ b而不使用其返回值。

它与myPoint.X相同;将发出警告,因为物业有

(不应该有)副作用。


-

cody


免费软件工具,游戏和幽默
http:// www.deutronium.de.vu || http://www.deutronium.tk

是的我明白你在说什么......只是不确定''NoSideEffect''

是描述它的最佳方式:)


如果编译器可以强制执行,那么实际上也是好的,即。

如果实现属性或函数的代码含有另一个警告

包含这个属性实际上是在类中修改其他属性或字段

,或者调用一个没有用这个属性标记的方法。


是的我很满意。


" cody" <无**************** @ gmx.net>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

是的,我能理解。我认为问题在于命名
方法。方法名称''Trim''就像''Replace''一样,暗示它实际上是

修改对象本身而不是返回修改过的对象。
也许如果它们带有前缀使用获取的方法并不是这样的


令人困惑。


虽然我喜欢描述方法行为的概念
一个属性,我不认为''NoSideEffect''或''RequiresUseReturnValue''
真的很好地解释了它。



此属性适用于如果在不使用返回值的情况下调用



方法,将发出警告的编译器。
它与myPoint.X相同;将发出警告,因为属性有(不应该)副作用。

-
cody

免费工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk


There should be a way to mark methods without sideeffects with a special
Attribute so that the compiler can recognize them and is able to issue a
warning:

public class string
{
[NoSideEffect()] // alternatively we could name it
[RequireUseReturnValue()]
public string Trim()
{
//...
}
}

this would be especially useful for immutable classes like string. A common
mistake is that you do:

myString.TrimEnd('','', '' '');

and as an effect, nothing happens since we forgot to assign the newly
created string.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk

解决方案

Yeah I can understand that. I think the problems lies in the naming of the
methods. The method name ''Trim'', just like ''Replace'', implies it''s actually
modifying the object itself rather than returning the modified object.
Perhaps if they prefixed the methods with ''Get'' it wouldn''t be so confusing.

While I like the concept of describing the behavior of the method with an
attribute, I don''t think ''NoSideEffect'' or ''RequiresUseReturnValue'' really
explain it well enough.

"cody" <no****************@gmx.net> wrote in message
news:u9**************@tk2msftngp13.phx.gbl...

There should be a way to mark methods without sideeffects with a special
Attribute so that the compiler can recognize them and is able to issue a
warning:

public class string
{
[NoSideEffect()] // alternatively we could name it
[RequireUseReturnValue()]
public string Trim()
{
//...
}
}

this would be especially useful for immutable classes like string. A common mistake is that you do:

myString.TrimEnd('','', '' '');

and as an effect, nothing happens since we forgot to assign the newly
created string.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk



> Yeah I can understand that. I think the problems lies in the naming of the

methods. The method name ''Trim'', just like ''Replace'', implies it''s actually modifying the object itself rather than returning the modified object.
Perhaps if they prefixed the methods with ''Get'' it wouldn''t be so confusing.
While I like the concept of describing the behavior of the method with an
attribute, I don''t think ''NoSideEffect'' or ''RequiresUseReturnValue'' really
explain it well enough.


This Attribute is for the compiler which will issue a warning if the method
is called without using its return value.
It is the same like myPoint.X; will issue a warning since properties have
(are should not have) sideeffects.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk


Yeah I understand what you''re saying... just wasn''t sure that ''NoSideEffect''
was the best way to describe it :)

It would actually also be good if the compiler could enforce that, ie.
issues another warning if the code implementing the property or function
containing this attribute were to actually modify other properties or fields
in the class, or to call a method that isn''t marked with this attribute.

Yeah I''m all for it.

"cody" <no****************@gmx.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Yeah I can understand that. I think the problems lies in the naming of the methods. The method name ''Trim'', just like ''Replace'', implies it''s actually

modifying the object itself rather than returning the modified object.
Perhaps if they prefixed the methods with ''Get'' it wouldn''t be so


confusing.


While I like the concept of describing the behavior of the method with an attribute, I don''t think ''NoSideEffect'' or ''RequiresUseReturnValue'' really explain it well enough.


This Attribute is for the compiler which will issue a warning if the


method is called without using its return value.
It is the same like myPoint.X; will issue a warning since properties have
(are should not have) sideeffects.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk



这篇关于没有副作用的方法(提案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆