...ing logging 4.0

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

D言語

enumに対するforeach

import std.stdio; import std.typecons; import std.typetuple; alias TypeTuple!( "Read", "Write", "Append" ) FileOpenModeDefinition; mixin(defineEnum!("FileOpenMode", FileOpenModeDefinition)); void main() { foreach (string element; FileOpenM…

OddElements!(T) / EvenElements!(T)

import std.typetuple; template OddElements(T...) { static if (T.length == 0) { alias TypeTuple!() OddElements; } else static if (T.length <= 2) { alias TypeTuple!(T[0]) OddElements; } else { alias TypeTuple!(T[0], OddElements!(T[2..$])) Od…

alias thisのフック

via. http://twitter.com/repeatedly/status/8021227522 import std.stdio; class A { B v; B get() { writeln("hook"); return v; } alias get this; } class B {} void main() { auto a = new A; a.v = new B; B b = a; // !!!! writeln(b); } hook a.Bこ…

type ident = void; と alias this による初期化遅延実験

import std.stdio; struct Int { private: int v; bool assigned; public: this(int i) { write(" [init] "); v = i; assigned = true; } int get() { if (!assigned) { write(" [init] "); v = 1; assigned = true; } return v; } void get(int i) { write(…

@property - dmd2.039

import std.stdio; class A{ @property int f() { return 0; } int g() { return 0; } } void main(){ A a = new A; writeln(a.f()); writeln(a.f); writeln(a.g()); writeln(a.g); } あれえー? @propertyなくても () を省略できてしまうぞ.

Re: C++ におけるコードレビューの重要性と活用

それD言語でできるよ! って言いたかったので考えてみた. より現実的な問題は,const_cast や mutable のような不穏なキーワードを伴わず,かつドキュメントと実際の動作が異なる場合です. #include <iostream> class LargeObj; LargeObj* g_obj; class LargeObj { p</iostream>…

Logger

import std.stdio; class Logger(T) { T loggee; this() { loggee = new T; } this(T loggee) { this.loggee = loggee; } auto opDispatch(string name, U...)(U args) { writeln("call " ~ name); mixin("return loggee." ~ name ~ "(args);"); } } class A…

DFL rev 81 + dmd 2.038

c:\d\dmd2\src\dfl\internal\winapi.d(2078): Error: long has no effect in expression (0)長らくこのエラーのせいでDFLが新しいdmdで使うことができなかったが,やっと原因がわかった. 他の人はすぐ直せていて困っていなかったのだろうか? 報告されてい…

構造体がコンストラクタを持つとき - 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 { i…

rvalueはconst refで受け取れない (2) - dmd 2.038

コンストラクタに引数があるときさらに問題があった. import std.stdio; struct X { static X opCall(int i) // こいつのせい.ローカル変数を外に出せないので戻り値型をref constにもできない { X x; return x; } // this(int i) // こうしましょう // { …

rvalueはconst refで受け取れない - dmd 2.038

けどopEqualsはref const引数を要求してくる. import std.stdio; struct X { bool opEquals(ref const(X) x) const { return true; } } struct Y { X getX() const { return X(); } } void main() { X x; Y y; writeln(x == y.getX); // Error: y.getX() is…

ViewあるいはPresentationと,Modelを分離する試み

import std.stdio; enum Message { OnDraw, OnChange, } class TextBoxVew /+ : GUIObjectView+/ { private string delegate() stringGetter; private void delegate(string) stringSetter; public this(string delegate() stringGetter, void delegate(stri…

dmd 2.037 (4)

import std.stdio; void main() { writeln(2.0^^10); } 1024

dmd 2.037 (3)

import std.stdio; void main() { int a, b; true?a:b = 1; writeln(a); writeln(b); } 1 0

dmd 2.037 (2)

import std.stdio; class A { private int value_; public @property { int value() {return value_;}; void value(int i){ value_ = i;}; } } void main() { auto a = new A; a.value = 2; a.value = a.value + 1; //a.value += 1; //error //a.value++; //…

dmd 2.037

Version D 2.037 Dec 3, 2009 New/Changed Features * Conditional expressions ?: can now be modifiable lvalues. * The type inferred from an ArrayLiteral is now a dynamic array, not a static one. * Added support for op= for array.length * Arra…

druntimeのtrunkをTortoiseSVNでエクスポートするURL

http://svn.dsource.org/projects/druntime/trunk いつも忘れるので.

最新のDFLはdmd2.031で動きます

動きます.

Rangeって何でこんなに楽しいのでしょうか

std::ostream_iterator的なものを. import std.stdio; import std.algorithm; //import std.range; struct TerminatorAdapter(Range, String)// if (isOutputRange!(Range, char)) // charだけじゃないんだけど... { Range r; String s; this(Range r, Stri…

TerminatorAdapter書き直し

import std.stdio; import std.algorithm; import std.range; struct TerminatorAdapter(Range, Element) if (isOutputRange!(Range, Element)) { Range r; Element terminator; this(Range r, Element terminator) { this.r = r; this.terminator = termina…

LockingTextWriter

LockingTextWriterはOutputRangeではないらしい,というか,ElementTypeを定義していればいいのかな?.どうにもならんのか. やっぱりOutputRangeでした.

配列演算で遊ぶ

import std.stdio; void main() { int[] a, b = [1,2,3,4,5]; a.length = b.length; // これわざわざユーザに書かせなくてもよくね? a[] = b[] * 2; //a = b[] * 2; // こうするとひどいことになるのが怖い writeln(a); }

ぶっこわしてやる

> type ice.d class A { void f(T...)() if (T.length != 1){} } void main() { A a = new A; a.f!int(); } > dmd main 2> error.txt // dmd crash >type error.txt //ice.d(3): Error: template ice.A.f(T...) if (T.length != 1) declaration T is already…

shared型修飾子のサンプルコード追加

http://rayerd.plala.jp/pukiwiki/ingwiki/index.php?C%2FC%2B%2B%E3%81%AB%E7%96%B2%E3%82%8C%E3%81%9F%E4%BA%BA%E3%81%AED%E8%A8%80%E8%AA%9E2.0 import std.stdio; import core.thread; int a; // スレッドごとに別々の静的変数を用意 shared int b; // …

「C/C++に疲れた人のD言語2.0」更新情報

http://rayerd.plala.jp/pukiwiki/ingwiki/index.php?C%2FC%2B%2B%E3%81%AB%E7%96%B2%E3%82%8C%E3%81%9F%E4%BA%BA%E3%81%AED%E8%A8%80%E8%AA%9E2.0 pure, synchronized,synchronizedメンバ関数,shared型修飾子についての記述を追加.

将来的にはテンプレート関数もオーバーロード可能に

テンプレートだと失敗するのは仕様? それは今後の課題で,将来的にはテンプレート関数もオーバーロード可能になるそうです. なんかソースのコメントに書いてあるとか.

synchronized その3

import std.stdio; import core.thread; class SharedData { private int v; // privateに変更 public synchronized void increase() { int a = v; for(int i; i<1000000; i++){} // wait v = a + 1; } public int value() const // constをなくすとエラー(…

synchronized その1.5

import std.stdio; import core.thread; class SharedData { public int v; public void increase() { // synchronizedだけだとグローバルなmutexが作成されるので, // 同期する必要がない他のsynchronizedブロックともお互いに処理待ちが発生してしまう sy…

synchronized その2

import std.stdio; import core.thread; class SharedData { public int v; public synchronized void increase() // 同期関数に変更 (2) { // 同期関数は次のようなsynchronized(this)と等価 (1) //synchronized(this){ int a = v; for(int i; i<1000000; i…

synchronized

import std.stdio; import core.thread; class SharedData { public int v; public void increase() { //synchronized // <== これの有無で結果が変わる { int a = v; for(int i; i<1000000; i++){} // wait v = a + 1; } } } class IncrementThread : Threa…