Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of D language?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the functions of D language". The content of the explanation is simple and clear, and it is easy to learn and understand. now please follow the editor's train of thought to study and learn "what are the functions of D language"?

If you have not installed the D language, please install the D language compiler so that you can run the D code yourself.

Look at the following sample code:

/ / file: ufcs_demo.d module ufcs_demo; import std.stdio: writeln; int [] evenNumbers (int [] numbers) {import std.array: array; import std.algorithm: filter; return numbers.filter! (n = > n% 2 = = 0). Array;} void main () {writeln (evenNumbers ([1,2,3,4]));}

Compile with your favorite D language compiler to see what this simple example application does:

$dmd ufcs_demo.d$. / ufcs_demo [2,4]

However, using UFCS as a built-in feature of the D language, you can also write code in a natural way:

Writeln ([1, 2, 3, 4]. EvenNumbers ());

Or completely remove the extra parentheses to make evenNumbers look like an attribute:

... writeln ([1,2,3,4] .evenNumbers); / / prints 2,4.

As a result, the complete code now becomes:

/ / file: ufcs_demo.d module ufcs_demo; import std.stdio: writeln; int [] evenNumbers (int [] numbers) {import std.array: array; import std.algorithm: filter; return numbers.filter! (n = > n% 2 = = 0). Array;} void main () {writeln ([1,2,3,4] .evenNumbers);}

Compile with your favorite D language compiler and give it a try. As expected, it produces the same output:

$dmd ufcs_demo.d$. / ufcs_demo [2,4]

During compilation, the compiler automatically takes the array as the first parameter of the function. This is a regular pattern that makes it fun to use the D language, so it's very similar to the way you naturally think about code. The result is functional programming.

You might guess what this print is:

/ / file: cool.dimport std.stdio: writeln;import std.uni: asLowerCase, asCapitalized; void main () {string mySentence = "D IS COOL"; writeln (mySentence.asLowerCase.asCapitalized);}

Make sure:

$dmd cool.d$. / coolD is cool

Combined with the functions of other D languages, UFCS enables you to write reusable code and write it naturally without sacrificing convenience.

Thank you for your reading, the above is the content of "what are the functions of D language". After the study of this article, I believe you have a deeper understanding of what the functions of D language are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report