- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我经常通过向其添加自引用(“自反”)类型参数约束来使一个简单的接口(interface)变得更加复杂。例如,我可能会这样:
interface ICloneable
{
ICloneable Clone();
}
class Sheep : ICloneable
{
ICloneable Clone() { … }
} //^^^^^^^^^^
Sheep dolly = new Sheep().Clone() as Sheep;
//^^^^^^^^
进入:
interface ICloneable<TImpl> where TImpl : ICloneable<TImpl>
{
TImpl Clone();
}
class Sheep : ICloneable<Sheep>
{
Sheep Clone() { … }
} //^^^^^
Sheep dolly = new Sheep().Clone();
主要优点:实现类型(例如 Sheep
)现在可以引用自身而不是其基类型,从而减少了类型转换的需要(如最后一行代码所示)。
虽然这非常好,但我也注意到这些类型参数约束并不直观,而且在更复杂的场景中往往会变得非常难以理解。*)
问题:有没有人知道另一种 C# 代码模式可以达到相同或类似的效果,但更容易掌握?
*) This code pattern can be unintuitive and hard to understand e.g. in these ways:
The declaration
X<T> where T : X<T>
appears to be recursive, and one might wonder why the compiler doesn't get stuck in an infinite loop, reasoning, "IfT
is anX<T>
, thenX<T>
is really anX<X<…<T>…>>
." (But constraints obviously don't get resolved like that.)For implementers, it might not be obvious what type should be specified in place of
TImpl
. (The constraint will eventually take care of that.)Once you add more type parameters and subtyping relationships between various generic interfaces to the mix, things get unmanageable fairly quickly.
最佳答案
Main advantage: An implementing type can now refer to itself instead of its base type, reducing the need for type-casting
虽然它可能看起来像是通过引用自身 的类型约束,它强制实现类型做同样的事情,但实际上它不是这样做的。人们使用此模式来尝试表达“此方法的覆盖必须返回覆盖类的类型”形式的模式,但这实际上并不是类型系统表达或强制执行的约束。我在这里举个例子:
https://ericlippert.com/2011/02/02/curiouser-and-curiouser/
While this is very nice, I've also noticed that these type parameter constraints are not intuitive and have the tendency to become really difficult to comprehend in more complex scenarios
是的。我尽量避免这种模式。很难推理。
Does anyone know of another C# code pattern that achieves the same effect or something similar, but in an easier-to-grasp fashion?
不在 C# 中,不。如果您对这类事情感兴趣,您可能会考虑查看 Haskell 类型系统; Haskell 的“高级类型”可以表示这些类型模式。
The declaration
X<T> where T : X<T>
appears to be recursive, and one might wonder why the compiler doesn't get stuck in an infinite loop, reasoning, "IfT
is anX<T>
, thenX<T>
is really anX<X<…<T>…>>
."
在推理如此简单的关系时,编译器永远不会陷入无限循环。但是,具有逆变的泛型的标称子类型通常是不可判定的。有一些方法可以迫使编译器无限倒退,而 C# 编译器不会检测到这些并在开始无限之旅之前阻止它们。 (但是。我希望在 Roslyn 编译器中添加对此的检测,但我们会看到。)
如果您对此感兴趣,请参阅我关于该主题的文章。您还需要阅读链接到的论文。
https://ericlippert.com/2008/05/07/covariance-and-contravariance-part-11-to-infinity-but-not-beyond/
关于c# - 自反类型参数约束 : X<T> where T : X<T> ‒ any simpler alternatives?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8866071/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我正在使用 gradle 1.4,并将 build.gradle 重命名为 buildExpr.gradle,将 settings.gradle 重命名为 settingExpr.gradle,这两个
使用 Ubuntu 16.04.7 我试图将 python3.8 作为我的默认 python 版本。我按照 here 给出的说明进行操作,但最终标准 python 版本没有改变。每当我运行 pytho
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 6年前关闭。 Improve this questi
我有一个函数,它对其参数进行模式匹配,以在 StateT () Maybe () 中生成计算。这个计算在运行时可能会失败,在这种情况下,我希望当前的模式匹配分支失败,可以这么说。 我非常怀疑是否可能有
我知道这类问题已经回答过几次,但我给出了问题的上下文以期待一些其他的架构替代方案。 考虑一个 CExpression 类: class CExpression { public:
当我的红色、绿色或蓝色变量发生变化时,我正在尝试使用 Combine 来更新颜色。我看过的示例使用 sink() ,这似乎适合我,但 eraseToAnySubscriber 是 MIA,我找不到替代
最近我不得不搜索一些字符串值来查看哪个匹配某个模式。在用户输入搜索词之前,字符串值的数量和模式本身都不清楚。问题是我注意到每次我的应用程序运行以下行时: if (stringValue.mat
我希望制作包含类对象列表的类对象的深拷贝,每个类对象都有自己的一组内容。对象不包含比整数和列表更令人兴奋的东西(没有字典,没有等待生成的生成器等)。我在一个循环中对 500-800 个对象执行深度复制
我正在尝试在表格中插入一个简单的行。有人可以指出这里发生了什么吗? CREATE TABLE recommendation_engine_poc.user_by_category (
使用 Get-ChildItem | Get-Member 我可以看到对象的方法和属性。但是我如何看到例如的不同可能值属性?我可以用 Get-ChildItem | Where-Object {$_.
我有一个任务是编写简单的解析器生成器,所以我编写了类似 ANTLR 的语法并尝试解析像“foo:bar;”这样的简单文件,但得到了以下输出: [@0,0:2='foo',,1:0] [@1,3:3='
这个问题已经有答案了: Why order matters in this RegEx with alternation? (3 个回答) Order of regular expression op
Ax 2009 中的 MorphX 报表设计器似乎不是“最好的”报表设计器。我不知道是不是我的错,或者 morphx 报表设计器是否有太多错误而无法完成他的工作。我猜是否有一些替代方案可以为 Ax 2
除了 OWASP XSS 过滤软件之外,还有其他方法可以防止 XSS 攻击吗?如果可以在 apache 级别进行阻止,我需要建议。我不是安全专家,因此需要详细信息。感谢您的帮助 最佳答案 当数据向最终
在我的 Java EE 7 程序中,我想使用 @Alternative 根据上下文、生产或测试来注入(inject)不同的实现。我所做的就是在 beans.xml 文件中声明用 @Alternativ
我有一个文本文件,其中包含遵循替代模式的行,例如: name: SomeName counterA: 0, counterB: 0, counterC: 0 name: SomeNameB count
我知道标准的单例模式是这样的: 原创 public class Singleton1 { public static Singleton1 _Instance; public stat
在 Haskell 中,您可以像这样为临时变量创建 where 条件: f x | cond1 x = a | cond2 x = g a | otherwise = f (h x
我认为我能找到的所有适用于 Android 的自定义按钮教程都假设您使用三种图像作为按钮:普通图像、按下图像和聚焦图像。 不是本质上将给定按钮资源的大小增加三倍(并为美工/UX 人员创造更多工作),是
我是一名优秀的程序员,十分优秀!