Conditions - Core kinds

The Ioke JVM Language: The power of Lisp and Ruby with an intuitive syntaxによると、Iokeでは"Condition"という特徴的な手法があるようなので、プログラミングガイドからとりあえず訳してみる。

http://ioke.org/guide.html#conditions

Conditions

One of the major parts of Ioke is the condition system. Unlike most other programming languages, Ioke doesn't have exceptions. Instead it has conditions, where errors is a specific kind of condition. The condition system comprises several different things. Specifically, the condition system uses the kinds Condition, Restart, Handler and Rescue. Restarts are mostly orthogonal to the rest of the system.

The way the condition system work is this. When something happens, a program can elect to signal a condition. This can be done using "signal!", "warn!" or "error!". Both "warn!" and "error!" uses "signal!" under the covers, but do some other things as well. A condition will always be an instance of a condition. Each of these three methods can be called in three different ways. First, you can call them with a text argument. In that case the signalled condition will be the default for that type. (The default for "signal!" is Condition Default. The default for "warn!" is Condition Warning Default and the default for "error!" is Condition Error Default). A new mimic of the default condition will be created, and a cell called text will be set to the text argument. The second variation is to give an instance of an existing condition to one of the methods. In that case that condition will be signalled unmodified. Finally, the third version gives a condition mimic and one or more keyword arguments with data to set on that condition. In that case a mimic of the condition will be created, and then cells with data set based on the arguments.

If a signal is not handled, nothing happens.

If a warning is not handled, a message will be printed with the text of that warning.

If an error is not handled, the debugger will be invoked - if a debugger is available. Otherwise the program will be terminated.

A Rescue allow conditions to unwind the stack to the place where the rescue is established. Combining rescues and conditions looks a lot like regular exception handling in other programming languages.

A Handler on the other hand will run code in the dynamic context of the place where the condition was signalled. A handler can invoke restarts to handle an error state at the place it happened, and as such doesn't have to actually unwind the stack anywhere. Any number of handlers can run - the last handler to run will be either the last handler, the handler that activates a restart, or the last handler before a valid rescue for that condition.

A restart is a way to allow ways of getting back to a valid state. Take the example of referring to a cell that doesn't exist. Before signalling a condition, Ioke will set up restarts so you can provide a value to use in the case a cell doesn't exist. This restart will use that new value and continue execution at the point where it would otherwise have failed.

状態処理

Iokeの大きな特徴の一つは、状態処理です。他の言語にあるような例外処理は、Iokeにはありません。代わりに状態処理を用い、エラーは状態の一種として扱います。コンディション処理は幾つか他の機能を含んでいます。特に、状態処理は"Restart""Handler""Rescue"という状態の種別を持っています。Restartは処理停止とは直行します*1

状態処理の手順は以下の通り。何かが起こった場合、プログラムは状態を表す信号を、"signal!"/"warn!"/"error!"より選択する。"warn!"と"error!"は"signal!"を包含するが、他は同様に動作する*2。ある状態は常に他の状態を継承している。3つのメソッドは3つの手順で呼ぶことができる。

  1. テキストを引数として呼ぶ。送出する状態はそれぞれのデフォルトタイプになる。("signal!"のデフォルトタイプはCondition Defalult、"warn!"はCondition Warning Default、"error!"はError Default。)
  2. 既に存在する状態のインスタンスをメソッドに与える。そのインスタンスを修正なしに送出する。
  3. 模擬状態と、いくつかのキーワードを引数として呼ぶ:模擬状態が生成され、引数に応じたデータセットとともに呼ばれる*3

signalは処理されなくても、何も起こらない。
warningが処理されなければ、状態に応じたメッセージを表示する。
errorが処理されなければ、(可能であれば)デバッガが起動する。もしくは、プログラムは停止される。

つづく。

"condition"を「状態」としたけど、「状態」といえば"status"を思い出させるかも。「条件」だと意味が広すぎるし、どうすればいいんだろか。

個人的には、JVMで動くプロトタイプベース言語は待望だった。

*1:

*2:

*3:"cells"は"calls"の間違い?