F#值屏蔽-是否可以在同一范围内禁用值屏蔽 [英] F# value shadowing - is it possible to disable value shadowing within the same scope

查看:118
本文介绍了F#值屏蔽-是否可以在同一范围内禁用值屏蔽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于复制/粘贴,我在自己的代码中发现了一个错误.在相同作用域中,相同的值名称会被复制/粘贴阴影.

I spoted a bug in my own code because of copy/paste. The same value name is shadowned by the copy/pasted in the same scope.

let func() =
    let a = 1
    let a = something_else
    ....

在C#中,我不会通过编译.有没有办法禁用阴影?至少在同一范围内?

In C# I wont pass compile. is there a way to disable shadowing? at least within the same level of scope?

谢谢

推荐答案

您无法在F#中禁用阴影-这是一项重要的语言功能.

You can't disable shadowing in F# -- it's an important language feature.

但是,您可以指示编译器发出警告或错误,这将帮助您捕获偶然阴影的情况.

However, you can instruct the compiler to issue a warning or error which will help you catch cases of accidental shadowing.

在项目属性中,添加 --warnon:1182 到其他标志"文本框(在构建"选项卡上,平台目标下拉菜单下).当您不小心阴影变量并导致在任何地方都不使用它时,将触发编译器警告.如果您希望这些情况导致编译失败,则还可以在构建"选项卡的将警告视为错误"部分下的特定警告"文本框中添加1182.

In the project properties, add --warnon:1182 to the "Other flags" textbox (on the Build tab, under the platform target dropdown). This triggers a compiler warning when you accidentally shadow a variable and cause it not to be used anywhere. If you'd rather these cases cause compilation to fail, you can also add 1182 to the "Specific warnings" textbox under the "Treat warnings as errors" section of the Build tab.

最后-请确实安装 Visual F#Power Tools 扩展.它提供了额外的语法高亮功能,并会指示未使用的变量,因此很容易在您的代码中发现它们.

Finally -- do install the Visual F# Power Tools extension. It provides additional syntax highlighting functionality and will indicate unused variables so they're easy to spot in your code.

这篇关于F#值屏蔽-是否可以在同一范围内禁用值屏蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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