謎's キッチン

謎のひとりごと。Amazon欲しい物リストはこちら: https://www.amazon.co.jp/hz/wishlist/ls/CCPOV7C6JTD2

基本型の制約

opCast_rが無いとどうしようも無いなぁ。取り合えず適当にパッチ作ってみた>opcast_r.diffgdcではうまく動いてる模様。

//gdc -fdebug
private import std.stdio;
template BaseType(T){
  alias int dummy;
  debug{
    class BaseType(T min,T max){
      T t;
      static typeof(this) opCast_r(int i){
        auto t=new typeof(this);
        t.t=i;
        return t;
      }
      int opCast(){
        return t;
      }
      /*色々省略*/
      invariant{
        assert(t<=max);
        assert(t>=min);
      }
    }
  }else{
    template BaseType(T min,T max){
      alias T BaseType;
    }
  }
}

void main(){
  auto a = cast(BaseType!(int).BaseType!(0,10))10;//opcast_r.diffが必要
  writefln(cast(int)a);
  a = cast(BaseType!(int).BaseType!(0,10))11;//ここでエラー起きてほしいのに
  writefln(cast(int)a);//ここでエラーが起こる不思議
}