Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I'm not sure this works either. It would seem I would have to define a validate function for each instance of a generic even if I'm not passing it in as a parameter.</p> </blockquote> <p>That is probably the <em>best</em> way to approach this; you're talking validation of data after all... and defaulting that could have unexpected behavior. (Any type that's crossing in/out of your system ought to be validated; e.g. File reads, DB reads, user-input, etc.)</p> <blockquote> <p>The point of having the default function in the generic was to eliminate duplicate code. Any other thoughts?</p> </blockquote> <p>Well, there is one method that springs to mind. It requires us to limit what we accept in our generic though. My other solutions are [almost] fully generalized and therefore would work on any non-limited type.</p> <p>Taking the attribute <code>'Valid</code> we see that the <a href="http://www.adaic.org/resources/add_content/standards/05aarm/html/AA-13-9-2.html" rel="nofollow">documentation</a> says this:<br> <code>The Valid attribute can be used to check the validity of data produced by unchecked conversion, input, interface to foreign languages, and the like.</code></p> <p>So we have a default validation, of sorts.<br> We also have a way to have attributes stand in for the default of function formal-parameters.<br> We have a way to default to visible functions.<br> Last we have nested generics.</p> <p>Now there's no generic formal type <code>scalar</code>, which would be useful here... but we have a way to limit the type of a formal parameter <code>Type T(&lt;&gt;) is (&lt;&gt;)</code> is limited to integral numerics, modular-types, and enumerations... all of which we know are scalar-types.</p> <pre><code>Generic Type Item(&lt;&gt;) is (&lt;&gt;); Package Generic_Base is -- To use this as a Ada-95 pkg delete everything after 'Boolean', -- create a body and return X'Valid from the implementation. Function Default_Validate(X : Item) Return Boolean is (X'Valid); Generic with function Validate(X:Item) return Boolean is Default_Validate; Package Generic_Nested is -- Operations requiring validation. End Generic_Nested; End Generic_Base; </code></pre> <p>Using these packages would be as follows:</p> <pre><code>Package Base is new Generic_Base( Integer ); Package Nested is new Base.Generic_Nested; </code></pre> <p>If you're happy in limiting the formal parameter to integrals/modulars/enumerations, this should work.</p> <p>So, there you go.</p> <hr> <p>Recommended reading: </p> <h2><a href="http://en.wikibooks.org/wiki/Ada_Programming/Generics#Generic_formal_types" rel="nofollow">Ada's Generic Formal Type System</a></h2> <hr> <p><sup>No syntax highlighting because it looks <strong><em>terrible</em></strong> on this.</sup></p>
 

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