オーバーロードはできません?(5)〜残余引数〜

残余引数を使うと、プロシージャやメソッドにおいて任意の数の引数を受け取ることができるようになります。
例えば、以下のように引数に任意の数のdouble値を受け取ってその合計を返すプロシージャを定義できます。

{define-proc {sum ...:double}:double
    let s:double = 0.0
    {for d in ... do
        set s = s + d
    }
    {return s}
}

{sum 1.5, 1.5, 3.7} ||6.7

{sum 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ||55.0


curlの標準APIでは、多くのGraphicサブクラスがコンストラクタにおいて残余引数を受け取るようになっています。これは主に、オプション(width, color, backgroundなど)の初期値設定や、イベントハンドラの指定に使われます。例えば、CommandButtonのコンストラクタ定義は以下のようになっています。

public {CommandButton.default
           label:#Label = "CommandButton",
           reactive-label:#ReactiveLabel = null,
           style:CommandButtonStyle = CommandButtonStyle.standard,
           text-breakable?:bool = false,
           ui-object:#CommandButtonUI = null,
           ...
}


CommandButtonをインスタンス化する際、コンストラクタの残余引数を通じて必要なだけのオプションとイベントハンドラを指定することができます。

{CommandButton
    label = "test",
    width = 30mm,
    color = "white",
    control-color = "black",
    font-weight = "bold",
    font-size = 20pt,
    tooltip = "click me",
    {on Action do
        {popup-message "ひとつ目のイベントハンドラ"}
    },
    {on Action do
        {popup-message "ふたつ目のイベントハンドラ"}
    }
}