ここからは新カテゴリ、D言語。
標準出力はwriteで行う。
まず、
Downloads - D Programming Language
ここからダウンロード。
$ curl -fsS https://dlang.org/install.sh | bash -s dmd Downloading https://dlang.org/d-keyring.gpg ######################################################################## 100.0% Downloading https://dlang.org/install.sh ######################################################################## 100.0% The latest version of this script was installed as ~/dlang/install.sh. It can be used it to install further D compilers. Run `~/dlang/install.sh --help` for usage information. Downloading and unpacking http://downloads.dlang.org/releases/2.x/2.084.0/dmd.2.084.0.linux.tar.xz ######################################################################## 100.0% Using dub 1.13.0 shipped with dmd-2.084.0 Run `source ~/dlang/dmd-2.084.0/activate` in your shell to use dmd-2.084.0. This will setup PATH, LIBRARY_PATH, LD_LIBRARY_PATH, DMD, DC, and PS1. Run `deactivate` later on to restore your environment. $
で、
$ cd dlang/ $ ls d-keyring.gpg dmd-2.084.0 install.sh $ cd ~ $ source $(~/dlang/install.sh dmd -a) (dmd-2.084.0)$ dmd --version DMD64 D Compiler v2.084.0 Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved written by Walter Bright (dmd-2.084.0)$ dub --version DUB version 1.13.0, built on Jan 2 2019 (dmd-2.084.0)$ deactivate $
dlangディレクトリの下にinstall.shがあるので、使う時はsourceでactivateして、終わるときはdeactivateするみたい。
dmdがコンパイラ、dubはパッケージ管理らしい。
(dmd-2.084.0)$ vi write.d (dmd-2.084.0)$ cat write.d import std.stdio; // 標準入出力 void main(){ write("ABCDE"); // writeで"ABCDE"を出力. } (dmd-2.084.0)$ dmd write.d write.d(3): Error: function expected before (), not module write of type void (dmd-2.084.0)$
こんな感じでstd.stdioをインポートして、writeで"ABCDE"を出力しようとしたが、なぜかコンパイルエラー・・・。
Wandboxでは、
いけたので、原因がまったくわからない・・・。
#dlang インストールスクリプトの使い方 - Kotet's Personal Blog
複数のバージョンを入れられるのでもしかしてとおもって、
$ ./install.sh install dmd-2.070.0 Downloading and unpacking http://downloads.dlang.org/releases/2.x/2.070.0/dmd.2.070.0.linux.tar.xz ######################################################################## 100.0% Downloading and unpacking https://github.com/dlang/dub/releases/download/v1.11.0-alpha.1/dub-v1.11.0-alpha.1-linux-x86_64.tar.gz ######################################################################## 100.0% Run `source ~/dlang/dmd-2.070.0/activate` in your shell to use dmd-2.070.0. This will setup PATH, LIBRARY_PATH, LD_LIBRARY_PATH, DMD, DC, and PS1. Run `deactivate` later on to restore your environment. $
2.084.0より古い2.070.0をインストール。
$ source ~/dlang/dmd-2.070.0/activate
アクティベートして、
あらためて、これをコンパイルすると、
$ cat write.d // モジュールのインポート import std.stdio; // 標準入出力 // main関数の定義 void main() // 戻り値はvoidでもいい. { // "ABCDE"を出力. write("ABCDE\n"); // writeで"ABCDE"を出力.(writeは改行しないので"\n"を付加.) } $ source ~/dlang/dmd-2.070.0/activate (dmd-2.070.0)$ ls write.d (dmd-2.070.0)$ dmd write.d (dmd-2.070.0)$ ls write write.d write.o (dmd-2.070.0)$ ./write ABCDE (dmd-2.070.0)$ deactivate $
このようにコンパイルも実行も成功した。
最新のはなぜかダメだった・・・。根本的な原因はなんだろうか・・・。
しばらくは安定している2.070.0でやっていき、問題なければ2.084.0でもやろう。
Sample/d/write/write/src/write at master · bg1bgst333/Sample · GitHub
(追記)
随分と時間が経ってしまいましたが、Twitterでのご指摘で解決しました。
端的に言うと
— B.G@あざちひぐち軍団所沢支部 (@miu_hiro_) 2019年1月22日
(dmd-2.084.0)$ cat write.d
import std.stdio; // 標準入出力
void main(){
write("a"); // writeで"ABCDE"を出力.
}
(dmd-2.084.0)$ dmd write.d
write.d(3): Error: function expected before (), not module write of type void
(dmd-2.084.0)$
これがおかしい
FF外から失礼しますが、D言語はデフォルトでソースファイルをモジュール(名前空間)にするので、ファイル名のwrite.dのwriteが優先されてエラーになっているようですね。
— 異国カラス (@outlandkarasu) 2019年1月22日
import https://t.co/1NZurkMO6Q : write;という形でインポートするかファイル名変えるかmodule宣言を行えば解消します
ありがとうございます。なるほど・・・。うーん・・・。自前のファイル名write.dがimportしてるモジュールと競合してるならわかるんですが、関数のwriteと競合するのはしっくりこないというか・・・。バージョンを古くしたら、動いたのも謎です・・・。
— B.G@あざちひぐち軍団所沢支部 (@miu_hiro_) 2019年1月22日
バグかもしれないのでちょっと調べましたが、2.071の下記の修正の影響を受けたみたいですね。https://t.co/UKvJIfvcTu
— 異国カラス (@outlandkarasu) 2019年1月22日
下記リファレンスに記載の通り、名前解決の順序が1. モジュール内 2. import の順になって、暗黙のmodule宣言はモジュール内と解釈されるようです。https://t.co/TgnswD1Ild