So I have a thing.
type Thing is new record
...elements...
end record;
I have a function which stringifies it.
function ToString(t: Thing) returns string;
I would like to be able to tell Ada to use this function for Thing'image
, so that users of my library don't have to think about whether they're using a builtin type or a Thing.
However, the obvious syntax:
for Thing'image use ToString;
...doesn't work.
Is there a way to do this?
I don’t know why the language doesn’t support this, and I don’t know whether anyone has ever raised a formal proposal that it should (an Ada Issue or AI). The somewhat-related AI12-0020 (the 20th AI for Ada 2012) includes the remark "I don't think we rejected it for technical reasons as much as importance”.
You can see why the Ada Rapporteur Group might think this was relatively unimportant: you can always declare an Image
function; the difference between
Pkg.Image (V);
and
Pkg.Typ’Image (V);
isn’t very large.
One common method is to create a unary + function...
function "+"(item : myType) return String;
which is syntactically very light. Obvious disclaimer: may lead to some ambiguity when applied to numeric types (e.g. Put (+4);)
However there is still the distinction between prebuilt types and user defined types. 'img wouldn't be able to be used by your client's code though, unless you specified an interface that enforced this function to be present (what if the client called on 'img for a private type that didn't have a 'img function defined?).
If you end up having to have an interface, it really doesn't matter what the function is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With