DLang 中的空字符串 属性

Empty property of a string in DLang

https://dlang.org/library/std/string/to_stringz.html 表示字符串有 empty 属性.

但事实并非如此,因为以下程序无法编译:

import std.stdio;

string s = "";

void main() {
  writeln(s.empty);
}

怎么了?

empty 属性 也是零长度字符串还是 null 字符串?我很困惑。

如果使用 null 参数调用,toStringz 会做什么?

我不认为 emptystring 的 属性,但它是可以应用于 ranges. Thanks to uniform function call syntax (UFCS) 的函数,它可能看起来像属性,但事实并非如此。以下编译并打印 true 三次:

import std.stdio;
import std.array : empty;

string s = "";

void main()
{
    writeln(empty(s)); // normal function call syntax
    writeln(s.empty()); // UFCS
    writeln(s.empty); // UFCS - empty parenthesis can be left out
}

这已经解释过,例如在 Programming in D:

Merely importing the std.array module makes the most common container type conform to the most capable range type: slices can seamlessly be used as RandomAccessRange objects.

The std.array module provides the functions empty, front, popFront() and other range functions for slices. As a result, slices are ready to be used with any range function.

It is not necessary to import std.array if the std.range module has already been imported.