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
不推荐.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现