Ada Programming/Attributes/'Value
From Wikibooks, open books for an open world
Contents |
function X'Value (Arg : String) return X'Base
[edit] Description
X'Value (Y) is an attribute where X is any discrete type and Y is a string. This attribute is a function that parses the string and returns the corresponding discrete type value, if it exists. Leading and trailing spaces are trimmed.
If the string does not correspond to any value of the discrete type, then a Constraint_Error exception is raised.
[edit] Example
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