私がそれだと思ったドキュメンテーションから私が言えることからinit
。なぜあなたがそれを使いたいのかはわかりませんが。クラスのすべてのインスタンスで使用される静的データの遅延ロードには役立つかもしれませんが、私はそれを自分で行ったことはありません。
オブジェクト指向プログラミングの用語では、クラスはオブジェクトとして作成されるときに「インスタンス化」されます。インスタンス化のプロセスは、「コンストラクター」でカスタマイズできます。Vala と Genie では、「デストラクタ」を使用して、オブジェクトが不要になったときのプロセスをカスタマイズすることもできます。
メソッドがオブジェクト内のデータに作用しない場合、それはstatic
メソッドと呼ばれます。定義上、コンストラクターはオブジェクト内のデータには作用しませんが、新しいオブジェクトを返します。したがって、コンストラクターは常に静的でstatic construct
あり、間違った名前です。Valaにはそれがclass construct
あり、それを使用するようにコードを変更すると、同じ結果が得られます。Valaでのこのテーマの完全な扱いについては、Vala のさまざまなタイプのコンストラクターを参照してください。についての部分class construct
は最後にあります。
私の理解では、Genie'sinit
は Vala's と同等でしたclass construct
。あなたの質問により、Genie の問題が明らかになったと思います。私の理解では、このコードはあなたの質問に答えるでしょう:
[indent = 4]
init
var a = new One()
var b = new One()
var c = new One.alternative_constructor()
class One:Object
init
print "Class 'One' is registered with GType"
construct()
print "Object of type 'One' created"
construct alternative_constructor()
print """Obect of type 'One' created with named constructor
'alternative_constructor', but the design of your
class is probably too complex if it has multiple
constructors."""
def One()
print "This is not a constructor"
final
print "Object of type 'One' destroyed"
ただし、これは次のように出力します。
Class 'One' is registered with GType
Object of type 'One' created
Class 'One' is registered with GType
Object of type 'One' created
Class 'One' is registered with GType
Obect of type 'One' created with named constructor
'alternative_constructor', but the design of your
class is probably too complex if it has multiple
constructors.
Object of type 'One' destroyed
Object of type 'One' destroyed
Object of type 'One' destroyed
GType への登録は一度しか行われないため、init
コードが間違った場所に配置され、型登録ではなくインスタンス化ごとに呼び出されます。class construct
したがって、現時点では、ジーニーにヴァラに相当するものはないと思います。
の動作を変更するinit
ことは 1 つの解決策かもしれませんが、元の目的を誤解している可能性があり、現在の動作に依存するコードが多数存在する可能性があります。もう 1 つの解決策は、これを実装する Genie パーサーのパッチを作成することです。この機能が便利な理由を説明する必要があります。