What is the usefulness of the `access` parameter mode?

ぃ、小莉子 提交于 2019-12-12 19:14:31

问题


There are three 'normal' modes of passing parameters in Ada: in, out, and in out. But then there's a fourth mode, accessis there anything wherein they're required? (i.e. something that would otherwise be impossible.)

Now, I do know that the GNAT JVM Ada-compiler makes pretty heavy use of them in the imported [library] specifications. (Also, they could arguably be seen as essential for C/C++ translations.)


回答1:


One of the primary drivers of the access mode was to work-around the restriction that, prior to Ada 2012, function parameters could only be of mode 'in'.

So while there may still be areas where they're an appropriate solution, perhaps in bindings, Ada 2012's relaxation of the allowed function parameters modes to now include 'in out' will probably significantly reduce the need for access mode.




回答2:


Regardless of what other uses there are for them, I rather like using them when coding bindings to C API's that take in pointers (if and only if 0 is not a valid value for that parameter on the C side).

This way on the Ada side I can deal with a nice object rather than a messy error-prone pointer.

Of course you can just specify in the bindings that the parameter is passed by reference, which gets you the same thing.




回答3:


In my latest project, the only time I've needed to use access so far is when defining my own stream subprograms (Read, Write, X'Class'Output etc. etc.). These functions require not null access Ada.Streams.Root_Stream_Type'Class as a parameter.

For example:

package Example is
    type Printable_Type is private;

    procedure Print_Printable(
        Stream : not null access Ada.Streams.Root_Stream_Type'Class;
        Print  : in Printable_Type);

    for Printable_Type'Write use Print_Printable;
end Example


来源:https://stackoverflow.com/questions/13146219/what-is-the-usefulness-of-the-access-parameter-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!