• Problems using Generic_Dispatching_Constructor

    From Mark Lorenzen@21:1/5 to All on Wed Jun 1 04:36:02 2022
    The generic function Ada.Tags.Generic_Dispatching_Constructor is defined as:

    generic
    type T (<>) is abstract tagged limited private;
    type Parameters (<>) is limited private;
    with function Constructor (Params : not null access Parameters) return T is abstract;

    function Ada.Tags.Generic_Dispatching_Constructor (The_Tag : Tag; Params : not null access Parameters) return T'Class;

    This gives us some problems when calling an instance of Ada.Tags.Generic_Dispatching_Constructor when the Params parameter is an in-mode parameter of a function e.g.:

    function Make (From_Params : in P) return T'Class
    is
    function Make_T_Class is new Ada.Tags.Ada.Tags.Generic_Dispatching_Constructor (T => T, Parameters => P, Constructor => ...);
    begin
    ...
    return Make_T_Class (Some_Tag, P'Access);
    end Make;

    This results in a compile-time error:
    error: access-to-variable designates constant

    Why is function Ada.Tags.Generic_Dispatching_Constructor defined as:
    function Ada.Tags.Generic_Dispatching_Constructor (The_Tag : Tag; Params : not null access Parameters) return T'Class;

    and not as e.g (note the access-to-constant type):
    function Ada.Tags.Generic_Dispatching_Constructor (The_Tag : Tag; Params : not null access constant Parameters) return T'Class;

    I guess we could declare function Make as (note the in-out mode):
    function Make (From_Params : in out P) return T'Class
    But this is horrible as functions should never ever have in-out or out-mode parameters (or side effects in general).

    Why are access types used at all?

    Is there another workaround?

    Regards,
    Mark L

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Mark Lorenzen on Wed Jun 1 14:42:40 2022
    On 2022-06-01 13:36, Mark Lorenzen wrote:

    Why are access types used at all?

    Parameters are kind of factory object, you want to have the factory mutable.

    Is there another workaround?

    In my practice I never had a case when I could obtain the tag needed for generic dispatching constructor. All my designs ended up with a mapping

    key -> constructing function

    with an explicit registering the type in the mapping.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Randy Brukardt@21:1/5 to Dmitry A. Kazakov on Wed Jun 1 16:25:16 2022
    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message news:t77mrv$1e20$1@gioia.aioe.org...
    On 2022-06-01 13:36, Mark Lorenzen wrote:

    Why are access types used at all?

    We needed this usable to implement dispatching stream attributes (the
    generic dispatching constructor was intended to be a user-definable generalization of the mechanism of the class-wide stream attribute). The
    stream attributes probably used access types because "in out" parameters
    were not allowed for functions when they were invented. (So mistakes piled
    on mistakes. :-)

    Parameters are kind of factory object, you want to have the factory
    mutable.

    Right. For instance, consider a factory where each object gets a unique ids while being constructed. You would want to update the Next_Id component at
    the end of each construction.

    Is there another workaround?

    In my practice I never had a case when I could obtain the tag needed for generic dispatching constructor. All my designs ended up with a mapping

    key -> constructing function

    with an explicit registering the type in the mapping.

    Right. Generally, one uses a mapping of some sort of key or menu choice or whatever to tags. If you aren't adverse to a giant case statement, then you might as well call the constructor directly. (And if you are willing to use access-to-functions, you don't need OOP at all.) So this "factory" is mostly
    a bone for OOP purists.

    The one exception is the case where you have an external tag as the key,
    since you can get the tag from that directly. But even that is really a
    mapping (one built by the implementation).

    Randy.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)