21用d编程可变参数

名字意思
__MODULE__模块名
__FILE__源文件名
__FILE_FULL_PATH__源文件完整路径
__LINE__行号
__FUNCTION__函数名
__PRETTY_FUNCTION__美化函数名
__DATE__编译日期
__TIME__编译时间
__TIMESTAMP__时间戳
__VENDOR__编译器厂商
__VERSION__版本

用法像:

import std.stdio;

void func(int parameter,
          string functionName = __FUNCTION__,
          string file = __FILE__,
          int line = __LINE__) {
    writefln("Called from function %s at file %s, line %s.",functionName, file, line);
}

void main() {
    func(42);    //
}
double sum(in double[] numbers...){//都是相同类型
    double result = 0.0;

    foreach (number; numbers) {
        result += number;
    }

    return result;
}

他们的参数生命期比较短.就在这个函数内.当然可以用.dup复制一份(实体)给生命期长的变量.
函数重载与c++差不多.
toString作为构/类的串表示.
常 引用表示该函数不会修改参数.所以可以传不变变量.=域 常当然也可以传不变变量.
在函数后的表示本成员函数不会修改对象.
不变对象上,显式调用非常函数,是通不过的.

    const string toString() {//const可前可后
        return format("%02s:%02s", hour, minute);
    }
    inout(int)[] firstPart(size_t n) inout {
        return elements[0 .. n];
    }//同样的

进出.
特殊函数:其中postblit要过时了.

    immutable a = S(1);//不变,自己能够推导
    auto b = immutable(S)(2);

都有点麻烦.不如不变 S a{...};
用户自己定义了构造器后,就使编译器自带的就无效了.
auto d=Duration (5);目前d的初化没c++优雅.
由于必须在编译时知道任何类型的初始值,所以不能显式定义默认构造函数.

struct Test {
    this() {//编译错误
        writeln("不能.");
    }
    static Test opCall() {
        writeln("明明是构造自己.");
        Test test;
        return test;
        //这里就不能写什么`Test()`了.
    }//新方法.
}
void main() {
    auto test = Test();//这样调用
}

d语言,折腾啊.

import std.stdio;

struct S {
    this(int i) {
        writeln("Constructing an object");
    }

    this(int i) const {
        writeln("Constructing a const object");
    }

    this(int i) immutable {
        writeln("Constructing an immutable object");
    }

    // We will see the 'shared' keyword in a later chapter.
    this(int i) shared {
        writeln("Constructing a shared object");
    }
}

void main() {//不对,
    auto m = S(1);
    const c = S(2);
    immutable i = S(3);
    shared s = S(4);
//要这样:
    auto m = S(1);
    auto c = const(S)(2);
    auto i = immutable(S)(3);
    auto s = shared(S)(4);
}
//-------
import std.stdio;

struct Student {
    const char[] fileName;
    int[] grades;

    this(const char[] fileName) {//常参数
        this.fileName = fileName;
    }

    void save() {
        auto file = File(fileName.idup, "w");
        file.writeln("The grades of the student:");
        file.writeln(grades);
    }

    // ...
}

void main() {
    char[] fileName;
    fileName ~= "student_grades";
    auto student = Student(fileName);
    fileName[] = 'A';//另一把钥匙搞事情
    student.save();//实体全变了.
}
//这样
struct Student {
    string fileName;
    // ...
    this(string fileName) {//不变
        // ...
    }
    // ...
}

类似c++自动类型转换,c++用.

struct Student {
    string name;

    this(string name) {
        this.name = name;
    }
}
void salute(Student student) {
    writeln("Hello ", student.name);
}
// ...
    salute("Jane");  //编译不过
import std.conv;
// ...
    salute(Student("Jane"));
    salute(to!Student("Jean"));
    salute(cast(Student)"Jim");
//显式转换.通过编译

禁用无参构造函数:

struct Archive {
    string fileName;

    @disable this();//@禁止
    this(string fileName) {//只允许这个
        // ...
    }
}

auto archive = Archive();,编译错误

    auto a = Archive("records");// <- 编译
    auto b = Archive.init;// <- 编译

析构器是自动执行的.
Postblit不推荐.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值