Swanman's Horizon

性欲をもてあましつつなんらかの話をするよ。

呼び出し規約を指定して無名メソッドを書く。

var
  func: TFunc<Integer,BOOL>;
begin
  func := // NG
    function(Data: Integer): BOOL stdcall
    begin
      Result := True;
    end;  

このように書こうとすると、コンパイラに「互換性ねーよ糞が」とか怒られるんだけど、そもそもTFunc<>の宣言を見てみると、

type
  TFunc<T,TResult> = reference to function (Arg1: T): TResult;

こうなってて、Delphiでは呼び出し規約の指定が無い場合は自動でregisterが指定されることになってる(と思う)ので、stdcallなどを指定した物を自分で作ってやれば呼び出し規約を変えられる。

type
  TStdcallFunc<T,TResult> = reference to function (Arg1: T): TResult stdcall;
var
  func: TFunc<Integer,BOOL>;
begin
  func := // OK
    function(Data: Integer): BOOL stdcall
    begin
      Result := True;
    end;