...ing logging 4.0

はてなブログに移行しました。D言語の話とかいろいろ。

構造体がコンストラクタを持つとき - dmd 2.038

import std.stdio;

struct S
{
	int x, y;
	this(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
}

class A
{
	const S s = {1, 2};
}

void main()
{
	A a = new A;
	writeln(a.s);
}
C:\d\projects\test>dmd main
Error: struct S has constructors, cannot use { initializers }, use S( initializers ) instead

エラーの行番号が出ないのと,エラーメッセージの出力先が標準エラー出力ではなく標準出力になっているのはバグだろう.

また,コンストラクタを作ると,const S s = {1, 2}; のような初期化はできなくなる.
そのときは,ちゃんとコンストラクタの中で初期化してあげればいい.

...
class A
{
	const S s;
	this()
	{
		s = S(1, 2);
	}
}
...

ちなみにコンストラクタを作ると const S s = {x:1, y:2}; みたいな初期化もできなくなる.
ううむ中途半端感.