Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, you can't--it's a chicken-and-egg problem. The compiler needs to figure out what all the generic parameters are going to be before it can instantiate the generic; but the <code>Default_Validate</code> method will not become available until <em>after</em> the generic is instantiated. The closest I think you can come is to declare two generics:</p> <pre><code>generic type Item is private; type Item_Ref is access all Item; with function Validate (Data_Ptr : Item_Ref) return Boolean; package Foo is function Default_Validate (Data_Ptr : Item_Ref) return Boolean; -- etc. end Foo; generic type Item is private; type Item_Ref is access all Item; package Foo_With_Default_Validator is -- important procedure/function declarations from Foo end Foo_With_Default_Validator; package body Foo_With_Default_Validator is function Default_Validate (Data_Ptr : Item_Ref) return boolean; package My_Foo is new Foo(Item, Item_Ref, Default_Validate); function Default_Validate (Data_Ptr : Item_Ref) return boolean renames My_Foo.Default_Validate; -- and other procedures/functions will be renames of things from My_Foo end Foo_With_Default_Validator; </code></pre> <p>(I haven't tested this yet. <b>EDIT:</b> tested, compiles OK.) I'm assuming here that the only publicly visible things in Foo are procedures and functions. If there are other important features (such as types), it gets more complicated, and then you might have to use nested generics, where <code>with function Validate</code> gets moved off the outer generic into an inner generic, or you might be able to use a generic formal package to split the generic into two parts. In either of those cases, the user of the generic might have to perform two instantiations. If the above solution works, then a user will instantiate either <code>Foo</code> or <code>Foo_With_Default_Validator</code>, but it would be one or the other--two instantiations wouldn't be needed. If you need more assistance, I think we'd need to see the visible part of <code>Foo</code>.</p> <p><b>EDIT 2: </b> Here's a solution if you're willing to require an <code>'Access</code> attribute when instantiating:</p> <pre><code>generic type Item is private; type Item_Ref is access all Item; Validate : access function (Data_Ptr : Item_Ref) return Boolean := null; package Foo is function Default_Validate (Data_Ptr : Item_Ref) return Boolean; -- etc. end Foo; </code></pre> <p>Then in the body of <code>Foo</code>, you'll want a function like this:</p> <pre><code>function Perform_Validate (Data_Ptr : Item_Ref) return Boolean is begin if Validate = null then return Default_Validate (Data_Ptr); else return Validate (Data_Ptr); end if; end Perform_Validate; </code></pre> <p>and call <code>Perform_Validate</code> from the rest of the body whenever you want to call the validation function. (<code>Perform_Validate</code> could be written more concisely using new Ada 2012 features, but you get the idea.) </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload