erlang @ Wiki

Macros

最終更新:

匿名ユーザー

- view
だれでも歓迎! 編集

1. Defining and Using Macros


A macro is defined the following way:
マクロの定義は以下のようにします:

-define(Const, Replacement).
-define(Func(Var1,...,VarN), Replacement).
  
A macro definition can be placed anyhere among the attributes and function declarations of a module, but the definition must come before any usage of the macro.
マクロ定義はモジュールの属性と関数が出来るみたいな。しかし、定義はマクロを使う前に必要だってかんじ。

If a macro is used in several modules, it is recommended that the macro definition is placed in an include file.

もしも、マクロがseveralモジュール内でつかうなら、これはマクロ定義はインクルードファイルないにあるとrecomendされる。

A macro is used the following way:
マクロは以下のように使われる:

?Const
?Func(Arg1,...,ArgN)
   

Macros are expanded during compilation. A simple macro ?Const will be replaced with Replacement. Example:
マクロはコンパイル時に展開される。シンプルなマクロ?Constは置き換えられるだろう。例:

-define(TIMEOUT, 200).
...
call(Request) ->
    server:call(refserver, Request, ?TIMEOUT).
   

This will be expanded to:
これは以下のように展開されるでしょう:
call(Request) ->
    server:call(refserver, Request, 200).
   

A macro ?Func(Arg1,...,ArgN) will be replaced with Replacement, where all occurences of a variable Var from the macro definition are replaced with the corresponding argument Arg. Example:
マクロ?Func(Arg1,...,ArgN)はバリアブルVarのマクロ定義からoccurencesの全ては置き換えられたcorresponding引数Argである、ところのReplacementによって置き換えられる。


-define(MACRO1(X, Y), {a, X, b, Y}).
...
bar(X) ->
    ?MACRO1(a, b),
    ?MACRO1(X, 123)
   

This will be expanded to:
これはこう展開されるでしょう:

bar(X) ->
    {a,a,b,b},
    {a,X,b,123}.


It is good programming practice, but not mandatory, to ensure that a macro definition is a valid Erlang syntactic form.
このことはよいプログラミングの練習です、しかし、mandatoryではなく、to ensure that a マクロ定義がvalid な Erlang シンタックスから。


To view the result of macro expansion, a module can be compiled with the 'P' option. compile:file(File, ['P']). This produces a listing of the parsed code after preprocessing and parse transforms, in the file File.P.
マクロexpansionの結果を見る、モジュールはPオプションによりコンパイルできる。
compile:file(File,['P']). このproduces パースされたコードのリスニング わからん。



2. Predefined Macros

The following macros are predefined:
以下のようなマクロがさいっしょっから定義されてる:

?MODULE
The name of the current module.
カレントモジュールの名前。
?MODULE_STRING.
The name of the current module, as a string.
カレントモジュールの名前の文字列。
?FILE.
The file name of the current module.
カレントモジュールのファイルの名前
?LINE.
The current line number.
カレント行番号。
?MACHINE.
The machine name, 'BEAM'.
マシン名、'BEAM'。

3. Flow Control in Macros

The following macro directives are supplied:
マクロディレクティブは以下のようなサプリード:

  • undef(Macro).
Causes the macro to behave as if it had never been defined.
Causes マクロ to behave as もしこれが定義されてなかったら。
  • ifdef(Macro).
Evaluate the following lines only if Macro is defined.
もしもマクロが定義されていれば以下の行が計算される。

  • ifndef(Macro).
Evaluate the following lines only if Macro is not defined.
マクロが定義されてなかったら以下の行が計算される。

  • else.
Only allowed after an ifdef or ifndef directive. If that condition was false, the lines following else are evaluated instead.
elseだよ。
  • endif.
Specifies the end of an ifdef or ifndef directive.
endifだろ。

Example:
例:
-module(m).
...

-ifdef(debug).
-define(LOG(X), io:format("{~p,~p}: ~p~n", [?MODULE,?LINE,X])).
-else.
-define(LOG(X), true).
-endif.

...


When trace output is desired, debug should be defined when the module m is compiled:
トレースしたいときー、debug should be 定義された モジュールmがコンパイルされたときー:
トレースしたいときは コンパイル時にdebugを定義しろってこった:
% erlc -Ddebug m.erl

or
とか

1> c(m, {d, debug}).
{ok,m}

なかんじで。

?LOG(Arg) will then expand to a call to io:format/2 and provide the user with some simple trace output.
?LOG(Arg)はまぁ、アレだ。io:format/2とシンプルなユーザーのトレース出力を含んだものに展開できるだろうと。

4. Stringifying Macro Arguments


The construction ??Arg, where Arg is a macro argument, will be expanded to a string containing the tokens of the argument. This is similar to the #arg stringifying construction in C.
??Argはなんかつくる。Argがマクロ引数のところ。展開 文字列を含む。引数トークン。これはシミュラーです。#arg文字列うんたらのCのなかみたいな。

The feature was added in Erlang 5.0/OTP R7.
未来は君のためにある。
Erlang 5.0/OTP R7から追加されたフューチャ

Example:
例:

-define(TESTCALL(Call), io:format("Call ~s: ~w~n", [??Call, Call])).

?TESTCALL(myfunction(1,2)),
?TESTCALL(you:function(2,1)).
   

results in
結果

io:format("Call ~s: ~w~n",["myfunction ( 1 , 2 )",m:myfunction(1,2)]),
io:format("Call ~s: ~w~n",["you : function ( 2 , 1 )",you:function(2,1)]).


That is, a trace output with both the function called and the resulting value.
このように、トレース出力は関数呼び出しのようにして結果をえられるとかそんなかんじ。

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

目安箱バナー