跳轉到內容

Ada 程式設計/屬性/'Value

來自華夏公益教科書,自由的教科書

Ada. Time-tested, safe and secure.
Ada. 經時間考驗,安全可靠。

'Value 屬性 為任何標量型別定義。此屬性是一個函式,解析字串並返回相應的型別值(如果存在)。開頭和結尾的空格將被修剪。

function S'Value (Arg: String) return S'Base;

如果字串與該型別中的任何值都不匹配,則會引發 Constraint_Error 異常

Wide_String 和 Wide_Wide_String 有相應的屬性。

不要將此屬性與 Ada 程式設計/屬性/'Val 屬性混淆。

type My_Enum is (Enum1, Enum2, Enum3);

pragma Assert (My_Enum'Value ("ENUM1")    = Enum1); -- OK
pragma Assert (My_Enum'Value ("  ENUM1 ") = Enum1); -- OK
pragma Assert (My_Enum'Value ("ZOWIE!")   = Enum1); -- Wrong! Constraint_Error is raised

華夏公益教科書

[編輯 | 編輯原始碼]

Ada 參考手冊

[編輯 | 編輯原始碼]
華夏公益教科書