これは、Brian Drummond が上記のコメントで書いたことの拡張です。私が Ada プログラミング言語で OOP を行い、いくつかのプライベート属性とパブリック属性を持つ「クラス」のアイデアを表現したい場合、次のように記述します (Matt の例を使用):
type Car_Type is tagged private;
function I_Am_A_Public_Attribute (This : Car_Type) return Integer;
function I_Am_Another_Public_Attribute (This : Car_Type) return Integer;
private
type Car_Type is tagged
record
I_Am_A_Public_Attribute : Integer;
I_Am_Another_Public_Attribute : Integer;
I_Am_A_Private_Attribute : Integer;
I_Am_Another_Private_Attribute : Integer;
end record;
function I_Am_A_Public_Attribute (This : Car_Type) return Integer is (This.I_Am_A_Public_Attribute);
function I_Am_Another_Public_Attribute (This : Car_Type) return Integer is (This.I_Am_Another_Public_Attribute);
アイデアは、公開したい属性ごとに get 関数を持つことです。実際、上記のコードは、私が「Ada スタイル」と呼ぶものではありません。Ada の強みを活用するには、各属性に新しい型を定義します。
type I_Am_A_Public_Attribute_Type is new Integer;
type I_Am_Another_Public_Attribute_Type is new Integer;
type Car_Type is tagged private;
function I_Am_A_Public_Attribute (This : Car_Type) return I_Am_A_Public_Attribute_Type;
function I_Am_Another_Public_Attribute (This : Car_Type) return I_Am_Another_Public_Attribute_Type;
private
type I_Am_A_Private_Attribute_Type is new Integer;
type I_Am_Another_Private_Attribute_Type is new Integer;
type Car_Type is tagged
record
I_Am_A_Public_Attribute : I_Am_A_Public_Attribute_Type;
I_Am_Another_Public_Attribute : I_Am_Another_Public_Attribute_Type;
I_Am_A_Private_Attribute : I_Am_A_Private_Attribute_Type;
I_Am_Another_Private_Attribute : I_Am_Another_Private_Attribute_Type;
end record;
function I_Am_A_Public_Attribute (This : Car_Type) return I_Am_A_Public_Attribute_Type is (This.I_Am_A_Public_Attribute);
function I_Am_Another_Public_Attribute (This : Car_Type) return I_Am_Another_Public_Attribute_Type is (This.I_Am_Another_Public_Attribute);
get 関数と間違った属性を混在させると、コンパイル時にエラーが発生することに注意してください。これは、「エイダ、強力なタイピングを信頼する」の良い例です。
編集:パフォーマンスの観点から、パブリック属性と取得関数の選択に好みがあるかどうかを調査したことがあります。GNAT コンパイラを使用した場合、パフォーマンスに差がないことがわかりました。他のコンパイラで同じ実験を試みたことはありません。