Ada-Rapporteur-Group / User-Community-Input

Ada User Community Input Working Group - Github Mirror Prototype
27 stars 1 forks source link

`'Valid` for enumerations #95

Open mosteo opened 5 months ago

mosteo commented 5 months ago

Would a 'Valid for an enumeration type that took a [[w]w]string would be interesting? To be able to know in advance if a 'Value is going to succeed or raise.

You can roll out your own with generics, of course, but I find this more often that I would have thought when dealing with user input.

Richard-Wai commented 5 months ago

I have definitely found myself wanting of this! It's particularly useful when you're trying to map enumeration values to hardware registers, or sharing enumerations across languages.

swbaird commented 5 months ago

I understand that this was just a preliminary sketch of a proposal, but that won't stop me from (probably prematurely) commenting on details. I think that extending the definition of the existing "Valid" attribute would be mistake. I think "Valid_Image", or "Is_Image", or even "Fred" would be a better name for this attribute.

joshua-c-fletcher commented 5 months ago

between overloading 'Valid or using 'Valid_Image, 'Is_Image, or 'Fred, I prefer 'Valid_Image for the name of the proposed attribute, with 'Is_Image coming in second.

This could be interesting for enum values that have constraints - either a range constraint or a predicate that mark certain values as invalid, because the image is Valid for the enum's 'Base type, in that case. But 'Valid would still return False for values omitted by a range constraint or a predicate, so we'd want this proposed attribute to return False for this cases too, or it couldn't serve the intent.

I agree this would be useful. Without it, we can just catch the exception, which is usually an exceptional case, but there are occasions when it would be nice to check before calling 'Value.

ArnaudCharlet commented 5 months ago

between overloading 'Valid or using 'Valid_Image, 'Is_Image, or 'Fred, I prefer 'Valid_Image for the name of the proposed attribute, with 'Is_Image coming in second.

This could be interesting for enum values that have constraints - either a range constraint or a predicate that mark certain values as invalid, because the image is Valid for the enum's 'Base type, in that case. But 'Valid would still return False for values omitted by a range constraint or a predicate, so we'd want this proposed attribute to return False for this cases too, or it couldn't serve the intent.

I agree this would be useful. Without it, we can just catch the exception, which is usually an exceptional case, but there are occasions when it would be nice to check before calling 'Value.

FWIW, GNAT has a 'Valid_Value attribute for that purpose: https://docs.adacore.com/gnat_rm-docs/html/gnat_rm/gnat_rm/implementation_defined_attributes.html#attribute-valid-value