メインコンテンツまでスキップ
バージョン: 次へ

メソッド

A 4D.Method object contains a piece of code that is created from text source and can be executed. 4D.Method methods always execute in interpreted mode, regardless of the project running mode (interpreted/compiled). This feature is especially designed to support dynamic, on-the-fly execution of code snippets.

A 4D.Method object is created with the 4D.Method.new() function.

4D.Method objects inherit from the 4D.Function class. Thus, to execute the method object, you can:

  • store a 4D.Method object in an object property and use the () operator after the property name,
  • or directly call the 4D.Method object using the call() or apply() function on it.

Function オブジェクト内のコードを実行する の段落の例題を参照してください。

例題

Basic dynamic method creation

var $myCode : Text
$myCode:="#DECLARE ($number1:Integer;$number2:Integer):Integer"+Char(13)+"return $number1*$number2"

var $o:={}
$o.multiplication:=4D.Method.new($myCode) //put object in a property
var $result2:=$o.multiplication(2;3) // 6

var $result3:=4D.Method.new($myCode).call(Null; 10; 5) // 50

Using This inside method code

var $myCode:="#DECLARE ($str1:text):text"+Char(13)+"return $str1+This.name"

var $o:={name: "John"}
$o.concat:=4D.Method.new($myCode)

var $result : Text
$result:=$o.concat("Hello ") // $result is "Hello John"

Using a text file with syntax checking

//4d method stored in a text file
var $newBusinessRules:=New shared object
Use ($newBusinessRules)
$newBusinessRules.taxRate:=0.2
$newBusinessRules.discountFormula:="price * quantity * discountRate"
$newBusinessRules.approvalThreshold:=10000
$newBusinessRules.freeShippingThreshold:=150
$newBusinessRules.defaultCurrency:="EUR"
End use

Use (Storage)
Storage.businessRules:=$newBusinessRules
End use

This method is called in the code:

var $myFile:=File("/DATA/BusinessRules.4dm")

var $myMethod:=4D.Method.new($myFile.getText())
// Syntax errors verification
If ($myMethod.checkSyntax().success)
$myMethod.call()
End if

Method Object

4D.Method objects provide the following properties and functions:

.apply() : any
.apply( thisObj : Object { ; params : Collection } ) : any

対象の function オブジェクトを、引数をコレクションとして渡して実行し、その結果の値を返します
.call() : any
.call( thisObj : Object { ; ...params : any } ) : any

.checkSyntax() : Object
checks the syntax of the source code of the 4D.Method object and returns a result object
.name : Text
contains the name of the 4D.Method object, if it was declared in the name parameter of the new() constructor
.source : Text
対象ファンクションのテキスト型のソースコード

4D.Method.new()

履歴
リリース内容
21 R3追加

4D.Method.new( source : Text {; name : Text } ) : 4D.Method

引数説明
sourceText->Textual representation of a 4D method to be encapsuled as an object
nameText->Name of the method to display in the debugger. If omitted, the method name will be displayed as "anonymous"
戻り値4D.Method<-New Method shared object

説明

The 4D.Method.new() function creates and returns a new 4D.Method object built from the source code.

In the source parameter, pass the 4D source code of the method as text. All end-of-line characters are supported (LF, CR, CRLF) using the Char command or an escape sequence.

In the optional name parameter, pass the name of the method to be displayed in the 4D debugger or Runtime explorer. If you omit this parameter, the method name will appear as "anonymous".

tip

Giving a name to your method is recommended if you want to:

The resulting 4D.Method object can be checked using checkSyntax() and executed using (), .apply() or .call().

Named volatile method objects are not project methods, they are not stored in disk files and cannot be called by commands such as EXECUTE METHOD. On the other hand, since they inherit from the 4D.Function class, they can be used wherever a 4D.Function object is expected.

例題

var $m:=4D.Method.new("#DECLARE ($t : Text) : Text \nreturn Uppercase($t)")

var $res:=$m.call(Null; "hello world") //HELLO WORLD

.apply()

履歴
リリース内容
21 R34D.Methods オブジェクトのサポート
17 R3追加

.apply() : any
.apply( thisObj : Object { ; params : Collection } ) : any

引数説明
thisObjObject->関数内での This コマンドによって返されるオブジェクト
paramsCollection->関数に引数として渡される値のコレクション
戻り値any<-関数の実行結果の値

説明

.apply() 関数は、対象の function オブジェクトを、引数をコレクションとして渡して実行し、その結果の値を返します。

thisObj には、関数内で This として使用されるオブジェクトへの参照を渡すことができます。 This を使用せず、しかし引数を渡したい場合には、ここに Null を渡します。

任意の params 引数を使用することで、フォーミュラ内で引数として使用されるコレクションを渡すこともできます:

  • 4D.Formula オブジェクトには、引数は $1...$n でフォーミュラに渡されます。
  • 4D.Method オブジェクトのようなその他の 4D.Function オブジェクトには、引数は宣言されたメソッド引数 内に渡されます。

.apply().call() と似ていますが、引数をコレクションとして渡す点が異なります。 これは計算された結果を渡すのに便利です。

例題

var $coll:=[10; 2]
var $myCode:="#DECLARE ($number1:Integer;$number2:Integer):Integer\n"+\
"return $number1*$number2"

$m:=4D.Method.new($myCode; "m_multiple")
var $result:=$m.apply(Null; $coll) //20

.call()

履歴
リリース内容
21 R34D.Methods オブジェクトのサポート
17 R3追加

.call() : any
.call( thisObj : Object { ; ...params : any } ) : any

引数説明
thisObjObject->関数内での This コマンドによって返されるオブジェクト
paramsany->関数に引数として渡される値
戻り値any<-関数の実行結果の値

説明

.call() 関数は、対象の function オブジェクトを、一つまたはそれ以上の引数を直接渡して実行し、その結果の値を返します。

thisObj には、関数内で This として使用されるオブジェクトへの参照を渡すことができます。

任意の params 引数を使用することで、フォーミュラ内で引数として使用される値を渡すこともできます:

  • 4D.Formula オブジェクトには、引数は $1...$n でフォーミュラに渡されます。
  • 4D.Method オブジェクトでは、引数は宣言されたメソッド引数 内に渡されます。

.call().apply() と似ていますが、引数を直接渡す点が異なります。

例題

 var $m : 4D.Method
var $myCode:="#DECLARE ($number1:Integer;$number2:Integer):Integer\n"+\
"return $number1*$number2"

$m:=4D.Method.new($myCode; "m_multiple")
var $result:=$m.call(Null; 10; 5) //50

.checkSyntax()

履歴
リリース内容
21 R3追加

.checkSyntax() : Object

引数説明
戻り値Object<-Syntax check result object

説明

The .checkSyntax() function checks the syntax of the source code of the 4D.Method object and returns a result object.

The Result object contains the following properties:

プロパティ説明
successBooleanTrue if no syntax error was detected, false otherwise
errorsObject の Collection以下はerror または warningの場合にのみ返されます。 Collection of objects describing errors or warnings
[].isErrorBooleanエラーならTrue、それ以外の場合は警告
[].messageTextError or warning message
[].lineNumberIntegerLine number of error in the code

例題

var $m : 4D.Method
var $check : Object
$m:=4D.Method.new("var $a:=2026\r$a:=current date")
$check:=$m.checkSyntax()
If ($check.success=False)
ALERT("Syntax error: "+$check.errors[0].message)
End if

.name

履歴
リリース内容
21 R3追加

.name : Text

説明

The .name property contains the name of the 4D.Method object, if it was declared in the name parameter of the new() constructor. Otherwise, the property is not returned.

このプロパティは 読み取り専用 です。

.source

履歴
リリース内容
21 R34D.Methods オブジェクトのサポート
18 R2追加

.source : Text

説明

.source プロパティは、対象ファンクションのテキスト型のソースコードを格納します。

返される値は4D.Formula または4D.Method オブジェクトを作成するのに使用された元のテキストですが、再フォーマットされます。

このプロパティは 読み取り専用 です。

例題

var $myCode:="#DECLARE ():Real\n"+\
"return random*current time"
$m:=4D.Method.new($myCode)
$src:=$m.source //"#DECLARE() : Real\rreturn Random*Current time"