在主源文件名中使用破折号

Using dash character in main source file name

我尝试用 llvm D 编译器编译一个文件 hello-world.d 并收到此消息:

Error: module hello-world has non-identifier characters in filename,
use module declaration instead

我查看了文档了解什么是模块声明,然后添加

module hello_world;

到我文件的顶部。然后编译了hello-world文件。请解释为什么需要它,如果我的主文件我应该使用什么模块名称。

在 D 中,标识符只能由字母、数字或下划线字符组成。模块名称也是一个标识符,包括其中的包。

对于拉丁字符集来说,字母字符是a-z和A-Z。非基于拉丁语的语言使用的其他字符集也可以使用(utf-8 支持)。

没什么特别的,但请记住,模块(包括包)需要匹配文件系统名称以进行查找(例如导入)。

简而言之 - 如果您不给模块命名,D 编译器将使用文件名(不带扩展名)作为模块名。

因此,如果您将 D 源文件命名为 hello-world.d,并且源文件顶部没有类似 module hello_world; 的内容,编译器实际上会尝试插入并编译module hello-world; 会失败,因为 hello-world 不是有效的标识符。

例如,如果文件名是 hworld.d,那么编译器实际上将编译一个具有自动生成名称 hworld.

的模块