D 语言的模块化、开发效率、可读性以及其它一些特性使其非常适合用于协同软件的开发。 -- Lawrence Aberba
本文导航
-模块化能力 …… 05%
-开发效率 …… 20%
-可读性和可维护性 …… 32%
-方便 …… 48%
-选择 …… 84%
-更多 …… 89%
编译自: https://opensource.com/article/17/5/d-open-source-software-development
作者: Lawrence Aberba
译者: ucasFL
D 语言的模块化、开发效率、可读性以及其它一些特性使其非常适合用于协同软件的开发。
import std.stdio; // 导入标准输入/输出模块
void main()
{
writeln("Hello, World!");
}
int count = 100_000_000;
double price = 20_220.00 + 10.00;
int number = 0x7FFF_FFFF; // 16 进制系统
// 返回偶数
int[] evenNumbers(int[] numbers)
{
// "filter" and "array" are only accessible locally
import std.algorithm: filter;
import std.array: array;
return numbers.filter!(n => n%2 == 0).array;
}
void cook(string food, int quantity)
{
import std.stdio: writeln;
writeln(food, " in quantity of ", quantity);
}
string food = "rice";
int quantity = 3;
cook(food, quantity);
string food = "rice";
int quantity = 3;
food.cook(quantity);
// Nope. Do you?
VeryLongTypeHere variable = new VeryLongTypeHere();
// 使用 auto 关键词
auto variable = new VeryLongTypeHere();
auto name = "John Doe";
auto age = 12;
auto letter = 'e';
auto anArray = [1, 2.0, 3, 0, 1.5]; // type of double[]
auto dictionary = ["one": 1, "two": 2, "three": 3]; // type of int[string]
auto cook(string food) {...} // auto for a function return type
foreach(name; ["John", "Yaw", "Paul", "Kofi", "Ama"])
{
writeln(name);
}
foreach(number; [1, 2, 3, 4, 4, 6]) {...}
foreach(number; 0..10) {...} // 0..10 is the syntax for number range
class Student {...}
Student[] students = [new Student(), new Student()];
foreach(student; students) {...}
int[] evenNumbers(int[] numbers)
{
import std.algorithm: filter;
import std.array: array;
return numbers.filter!(n => n%2 == 0).array;
}
unittest
{
assert( evenNumbers([1, 2, 3, 4]) == [2, 4] );
}
欢迎光临 51学通信论坛2017新版 (http://bbs.51xuetongxin.com/) | Powered by Discuz! X3 |