有没有一种方法可以在Visual Studio中为F#中的阴影值发出警告? [英] Is there a way to have warnings for shadowing values in F# in Visual Studio?

查看:105
本文介绍了有没有一种方法可以在Visual Studio中为F#中的阴影值发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,现有值的阴影如下:

To me shadowing of existing values like described in:

阴影和嵌套函数
在F#中不可变
f#重复定义
通过FSharp获得乐趣和获利评论

Shadowing and Nested function
immutable in F#
f# duplicate definition
FSharp for fun and profit comment

似乎与使F#如此强大的不变性和类型安全性概念背道而驰. F#中的阴影效果不同比在C#中.我花了相当多的时间才发现我的代码中的错误是由于同一范围内名称的无意阴影所致.有没有一种方法可以对VS中的阴影值产生编译器警告?

seems to be going against the notion of immutability and type safety that makes F# so strong. Shadowing in F# works different than in C#. It just took me quite some time to find out that a bug in my code was due to unintentional shadowing of a name within the same scope. Is there a way to have compiler warnings for shadowing values in VS?

我知道在某些情况下它会很有用.例如 Checked Aritmetics .

I know that in some cases it can be useful. for example for Checked Aritmetics .

推荐答案

阴影具有优点和缺点.我也遇到过由于手指操作繁琐而产生的错误.从好的方面来说,它可以帮助您保持变量空间的清洁,如@JoelMueller所指出的.

Shadowing has pros and cons. I too have encountered bugs due to fat-fingered shadowing efforts. On the plus side, it can help keep your variable space clean as @JoelMueller pointed out.

阴影错误与可变变量错误本质上是不同的.它们是拼写错误.它们更易于分析:历史信息的损失在字典环境和环境环境之间被最小化.就是说,有了阴影,您总是可以通过精神堆栈展开来清晰地跟踪绑定的值,而变量突变会产生本质上是混乱的东西(跳转到地址).

Shadowing bugs are fundamentally different than mutable variable bugs. They are of the typo variety. They are much easier to analyze: historical information loss is minimized to lexicographical context versus environmental context. That is, with shadowing, you can always cleanly trace the value of a binding through mental stack unrolling, whereas variable mutations create what are essentially gotos (jump to address).

实际上,阴影仍然可以消除所有类型的错误.您不会遇到任何远距离的怪异动作".即,您不会遇到闭包捕获的变量或相对于当前作用域在嵌套作用域中被修改的变量的问题.

Practically, shadowing still eliminates whole classes of bugs. You won't encounter any "spooky actions from a distance". Namely, you won't run into issues with variables captured in closures or variables being modified in nested scopes relative to the current scope.

这篇关于有没有一种方法可以在Visual Studio中为F#中的阴影值发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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