InExpression


Ver 0.160 がリリースされた。
更新履歴を読む。

Added operator overloading of InExpression.

ほー、in 式 のオーバーロードがサポートされたんだ。
連想配列をラッピングしたクラス定義が完璧にできるな。


さっそくお試し。

import std.stdio;
import std.string;
import house.tostring;

class Map(K, V) {
	private V[K] map;
	this() {}
	
	K[] keys() { return map.keys; }
	V[] values() { return map.values; }
	
	char[] toString() { return map.ToString(); }

	V opIndex(K key) { return map[key]; }
	V opIndexAssign(V value, K key) { return map[key] = value; }
	V* opIn(K key) { return (key in map); }
}

void main() {
	alias Map!(char[], int) CIMap;
	CIMap map = new CIMap();
	map["AAA"] = 1;
	map["BBB"] = 2;
	map["CCC"] = 3;
	writefln(*("BBB" in map));
}
...
C:\d\test>dmd -run map
map.d(25): rvalue of in expression must be an associative array, not map.Map!(ch
ar[],int).Map
map.d(25): can only * a pointer, not a 'int'

なんで?

in 式の多重定義サポートしたんでないの?

	writefln(*map.opIn("BBB"));
...
2

これはさすがに通るな。

	writefln(*map.in("BBB"));

これはだめ。

	writefln(*(map in "BBB"));

なんとこれが通ってしまった。
え〜、連想配列連想配列もどきクラスで in 式の順序が逆になんの〜。


in ではなくて includes または contains って感じだな〜。


これじゃ opIn のオーバーロードを禁止して、opIn_r を定義させた方がいんでないかい?