mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translating
This commit is contained in:
parent
914eb98c6b
commit
b89b57214c
@ -7,17 +7,16 @@
|
||||
[#]: via: (https://opensource.com/article/20/7/d-programming)
|
||||
[#]: author: (Lawrence Aberba https://opensource.com/users/aberba)
|
||||
|
||||
The feature that makes D my favorite programming language
|
||||
让 D 语言成为我最喜欢的编程语言的功能
|
||||
======
|
||||
UFCS gives you the power to compose reusable code that has a natural
|
||||
flow without sacrificing convenience.
|
||||
UFCS 能让你能够编写自然的可重用代码而不会牺牲便利性。
|
||||
![Coding on a computer][1]
|
||||
|
||||
Back in 2017, I wrote about why the [D programming language is a great choice for development][2]. But there is one outstanding feature in D I didn't expand enough on: the [Universal Function Call Syntax][3] (UFCS). UFCS is a [syntactic sugar][4] in D that enables chaining any regular function on a type (string, number, boolean, etc.) like its member function of that type.
|
||||
早在 2017 年,我就写了为什么 [D 语言是开发的绝佳选择][2]的文章。但是 D 语言中有一个杰出功能,我没有充分扩展:[通用函数调用语法][3](UFCS)。UFCS 是 D 语言中的[语法糖][4],它可以在类型(字符串、数字、布尔值等)上链接任何常规函数,例如该类型的成员函数。
|
||||
|
||||
If you don't already have D installed, [install a D compiler][5] so you can [run the D code][6] in this article yourself.
|
||||
如果你尚未安装 D 语言,请[安装 D 语言编译器][5],以便你可以自己[运行 D 代码][6]。
|
||||
|
||||
Consider this example code:
|
||||
考虑以下示例代码:
|
||||
|
||||
|
||||
```
|
||||
@ -41,7 +40,7 @@ void main()
|
||||
}
|
||||
```
|
||||
|
||||
Compile this with your favorite D compiler to see what this simple example application does:
|
||||
使用你喜欢的 D 语言编译器进行编译,查看这个简单示例应用做了什么:
|
||||
|
||||
|
||||
```
|
||||
@ -50,7 +49,7 @@ $ ./ufcs_demo
|
||||
[2, 4]
|
||||
```
|
||||
|
||||
But with UFCS as a built-in feature of D, you can also write your code in a natural way:
|
||||
但是,使用作为 D 语言的内置功能的 UFCS ,你还可以自然方式编写代码:
|
||||
|
||||
|
||||
```
|
||||
@ -59,7 +58,7 @@ writeln([1, 2, 3, 4].evenNumbers());
|
||||
...
|
||||
```
|
||||
|
||||
or completely remove the now-redundant parenthesis to make it feel like `evenNumbers` is a property:
|
||||
或完全删除现在多余的括号,使 “evenNumbers” 看起来像是一个属性:
|
||||
|
||||
|
||||
```
|
||||
@ -68,7 +67,7 @@ writeln([1, 2, 3, 4].evenNumbers); // prints 2, 4
|
||||
...
|
||||
```
|
||||
|
||||
So the complete code now becomes:
|
||||
因此,完整的代码现在变为:
|
||||
|
||||
|
||||
```
|
||||
@ -92,7 +91,7 @@ void main()
|
||||
}
|
||||
```
|
||||
|
||||
Compile it with your favorite D compiler and try it out. As expected, it produces the same output:
|
||||
使用你最喜欢的 D 语言编译器进行编译,然后尝试一下。 如预期的那样,它产生相同的输出:
|
||||
|
||||
|
||||
```
|
||||
@ -101,9 +100,9 @@ $ ./ufcs_demo
|
||||
[2, 4]
|
||||
```
|
||||
|
||||
During compilation, the compiler _automatically_ places the array as the first argument to the function. This is a regular pattern that makes using D such a joy, so it very much feels the same as you naturally think about your code. The result is functional-style programming.
|
||||
在编译过程中,编译器_自动地_将数组作为函数的第一个参数。 这是一个常规模式,使得使用 D 语言成为一种乐趣,因此,它与你自然考虑代码的感觉非常相似。 结果就是函数式编程。
|
||||
|
||||
You can probably guess what this prints:
|
||||
你可能会猜出打印:
|
||||
|
||||
|
||||
```
|
||||
@ -118,7 +117,7 @@ void main()
|
||||
}
|
||||
```
|
||||
|
||||
But just to confirm:
|
||||
确认一下:
|
||||
|
||||
|
||||
```
|
||||
@ -127,11 +126,11 @@ $ ./cool
|
||||
D is cool
|
||||
```
|
||||
|
||||
Combined with [other D features][7], UFCS gives you the power to compose reusable code that has a natural flow to it without sacrificing convenience.
|
||||
结合[其他 D 语言的功能][7],UFCS 使你能够编写可重用的代码,并在不牺牲便利性的情况下自然地进行编写。
|
||||
|
||||
### Time to try D
|
||||
### 是时候尝试 D 语言了
|
||||
|
||||
As I've written before, D is a great language for development. It's easy to install from [the D download page][8], so download the compiler, take a look at the examples, and experience D for yourself.
|
||||
就像我之前写的那样,D 语言是一种很棒的开发语言。从 [D 语言的下载页面][8]可以很容易地进行安装,因此请下载编译器,查看示例,并亲自体验 D 语言。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -139,7 +138,7 @@ via: https://opensource.com/article/20/7/d-programming
|
||||
|
||||
作者:[Lawrence Aberba][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user