滥用断言
介绍 (Introduction)
Yet another crazy way to write if-then
statements without writing if.
Really, don't do this, it's just an experiment in what's possible. The main reason not to do this is that extension methods and the ?.
null continuation operator were not intended for this purpose, and you'll confuse the heck out of other programmers when they encounter this crazy code.
编写if-then
语句而不编写if.
另一种疯狂方式if.
真的,不要这样做,这只是对可能的实验。 不这样做的主要原因是扩展方法和?.
空连续运算符不是用于此目的的,当其他程序员遇到这个疯狂的代码时,您会感到困惑。
基本的如果 (The Basic If)
The if
statement is one of the cornerstones of a program, slightly more readable than its assembly language counterparts "branch if equal", "branch if not equal", "branch if greater", and so forth. In almost every language, you write the following, which tests the truth of the condition:
if
语句是程序的基石之一,比其汇编语言对应的“如果相等则分支”,“如果不相等则分支”,“如果更大则分支”等更具可读性。 几乎每种语言都编写了以下代码,用于测试条件的真实性 :
if (somethingIsTrue)
{
doSomethingWhenTrue();
}
For our little demonstration, we'll use this code:
对于我们的小示范,我们将使用以下代码:
public void Test()
{
if (true)
{
Write("The usual way");
}
}
public void Write(string msg)
{
Console.WriteLine(msg);
}
So far so good. Everyone can read that, even those without any programming experience!
到目前为止,一切都很好。 每个人都可以阅读,即使没有任何编程经验的人也可以阅读!
当真 (WhenTrue)
Now let's introduce an extension method:
现在让我们介绍一个扩展方法:
public static void WhenTrue(this bool condition, Action action)
{
if (condition)
{
action();
}
}
And now, using extension and anonymous methods, we can write:
现在,使用扩展名和匿名方法,我们可以编写:
true.WhenTrue(() => Write("Using an extension method"));
Now we have something less readable unless you're familiar with anonymous methods (aka "fat arrow" methods) but you still get the basic idea -- The anonymous method, as an Action
, is executed when the condition on the left is true, and:
现在,除非您熟悉匿名方法(又称“胖箭头”方法),否则我们的可读性将有所降低,但是您仍然可以了解基本概念-当左侧的条件为true时,将匿名方法作为Action
执行,和:
false.WhenTrue(() => Write("Should be false!!!"));
When not true, it doesn't. This works of course:
如果不正确,则不是。 这当然起作用:
int a = 1;
int b = 1;
(a == b).WhenTrue(() => Write("A equals B"));
But the () =>
syntax is a lot of arcane symbol incantation and what is occult in the anonymous method is that there is a hidden closure on this
, such that the Write
method is found in the class making the call.
但是, () =>
语法是很多神秘的符号咒文,什么是隐匿在匿名方法是,有一个隐藏的关闭this
,使得Write
方法是在类中进行调用。
如果不为空,则继续 (Continue if Not Null)
If we introduce a different extension method:
如果我们引入其他扩展方法:
public static T WhenTrue<T>(this bool condition, T me) where T : class
{
return condition ? me : default(T);
}
lo-and-behold, we can write:
看得出来,我们可以这样写:
true.WhenTrue(this)?.Write("Using null continuation");
false.WhenTrue(this)?.Write("Should also be false!!!");
Oh my. Now we have entered into the land of occult initiation that the materialist developer cannot immediately understand and will (with good reason) deny even exists! Why are we passing in this
? Why does the extension method return null
? How is it possible to continue with a method of the class?
天啊。 现在,我们进入了一个神秘的领域,唯物主义的开发者无法立即理解它,并且(有充分的理由)将否认甚至存在! 我们为什么要传递this
? 为什么扩展方法返回null
? 如何继续使用该类的方法?
In the occult school of programming, the beauty of this
is that this
is returned by the extension method when the condition is true
, and with the arcane knowledge that T
is a class, default(T)
always returns null!
在节目的神秘学校的美女this
是 this
是由扩展方法返回时的条件为true
,并与神秘的知识T
是一个类, default(T)
总是返回null!
您想在哪里? (Where Do You Want This?)
The above syntax is sort of backwards, particularly since this
is a parameter of WhenTrue
, which makes reading the code a bit like reading Reverse Polish Notation. Let's try this instead:
上面的语法有点倒退,特别是因为this
是WhenTrue
的参数,这使得读取代码有点像读取反向波兰表示法。 让我们试试这个 :
public static T WhenTrue<T>(this T me, bool condition) where T : class
{
return condition ? me : default(T);
}
Funny how the implementation of the extension method is exactly the same, all we've done is swapped the parameters such that instead of the extension method being available to a bool
type, it's available to any class type. So now we can write something a wee bit more left-to-right:
滑稽的扩展方法的实现如何是完全一样的,我们所做的是交换的参数,从而代替扩展方法是提供给bool
型,它可用于任何类类型 。 所以现在我们可以从左到右写一些东西:
this.WhenTrue(true)?.Write("Another truth");
this.WhenTrue(false)?.Write("Another falsehood");
合并您的提升或其他! (Coalesce Your Ascension or Else!)
So what about the else
, a necessary kindred spirit of if
? Well, try this, using the null
coalescence operator:
那么, else
一种, if
是必要的亲属精神呢? 好吧,使用null
合并运算符尝试一下:
var _ = this.WhenTrue(true)?.Write("Another truth") ?? Write("Not truth");
var __ = this.WhenTrue(false)?.Write("Another falsehood") ?? Write("False!");
This bit of magic requires that Write
returns a value and that we assign that value to something we probably don't care about, so Write
now looks like this:
这点魔术要求Write
返回一个值,并且我们将该值分配给我们可能不关心的东西,因此Write
现在看起来像这样:
public int Write(string msg)
{
Console.WriteLine(msg);
return 0;
}
Truly, we have entered into the realm of the esoteric!
确实,我们已经进入了深奥的领域!
因此 ... (And Thus...)
We conclude the year 2019 with how extension methods and null
continuation operators can ascend to do something the designers probably never intended.
我们以可扩展方法和null
连续运算符如何提升设计师可能从未打算做的事情来结束2019年。
The grand master of coding will also note that statements like:
编码大师也将注意以下语句:
this.WhenTrue(true)?.Console.WriteLine("Go to h***");
are not possible because, of course, Console
is not a member of this
unless you write a static member of this
called Console
that wraps the functionality of System.Console
...run away!
是不可能的,因为,当然, Console
是不是成员this
除非你写的静态成员this
所谓的Console
,它包装的功能System.Console
......跑了!
翻译自: https://www.codeproject.com/Tips/5255310/Abusing-Extension-Methods-and-Null-Continuation
滥用断言