* When to use formal discrete type?
@ 2017-11-29 19:04 Victor Porton
2017-11-29 19:43 ` Shark8
2017-11-29 21:05 ` Randy Brukardt
0 siblings, 2 replies; 4+ messages in thread
From: Victor Porton @ 2017-11-29 19:04 UTC (permalink / raw)
I am creating a generic package which accepts as a formal argument a type
which is expected to be an enumeration type.
Should for this enumeration argument I use formal private type (is private)
or formal discrete type (is (<>))?
I do not use features specific to discrete types such as 'First or 'Range
attributes.
Because it easily generalizes from discrete types to arbitrary private
types, should I do generalization?
What of (generalizing for all private types or not) the two is better:
a. for clarity of code;
b. for performance?
It there a noticeable difference in performance if I generalize?
--
Victor Porton - http://portonvictor.org
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: When to use formal discrete type?
2017-11-29 19:04 When to use formal discrete type? Victor Porton
@ 2017-11-29 19:43 ` Shark8
2017-11-29 21:07 ` Dmitry A. Kazakov
2017-11-29 21:05 ` Randy Brukardt
1 sibling, 1 reply; 4+ messages in thread
From: Shark8 @ 2017-11-29 19:43 UTC (permalink / raw)
On Wednesday, November 29, 2017 at 12:04:35 PM UTC-7, Victor Porton wrote:
>
> Because it easily generalizes from discrete types to arbitrary private
> types, should I do generalization?
>
> What of (generalizing for all private types or not) the two is better:
>
> a. for clarity of code;
>
> b. for performance?
>
> It there a noticeable difference in performance if I generalize?
My rule of thumb is use the most general type as reasonably possible -- so, if you aren't using things like X'First and X'Succ(Value) go ahead and make the parameter private. (On the other hand, if you do find yourself using a value for first/last and/or functions for previous/next consider using more restrictive generic formal parameter; of course, your design might make it impossible to use a discrete-formal [like, say, modeling integrals (for certain models)].)
As for performance, I would recommend that you first benchmark and hotspot-analyze your code: it's typically dumb to spend hours and hours super-optimizing something from O(n**4) to O(n) if n always <= 3. -- Chances are that your intuition as to what's a /real/ performance hit is going to be wrong.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: When to use formal discrete type?
2017-11-29 19:04 When to use formal discrete type? Victor Porton
2017-11-29 19:43 ` Shark8
@ 2017-11-29 21:05 ` Randy Brukardt
1 sibling, 0 replies; 4+ messages in thread
From: Randy Brukardt @ 2017-11-29 21:05 UTC (permalink / raw)
"Victor Porton" <porton@narod.ru> wrote in message
news:ovn0bv$1rto$1@gioia.aioe.org...
...
> It there a noticeable difference in performance if I generalize?
That clearly is compiler specific, and also specific to how the type is
used.
Janus/Ada, for example, uses generic sharing, so the code generated for a
formal discrete type and a formal private type are very different. (In most
cases, the formal private type will win, but not if there are a lot of
components of that type).
OTOH, GNAT generally uses template expansion. In such a case, the final code
isn't much different than if you had written it yourself without using the
generic. So it probably doesn't matter much.
The main difference is the promise given to the instantator. A private type
implies that any non-limited type will work (assuming that it has the
operations given in the generic spec). So you may want to consider what
happens if someone instantiates your generic with some record type. If that
would be a real problem, then the formal discrete type would be better (as
it better documents what actually works).
Randy.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: When to use formal discrete type?
2017-11-29 19:43 ` Shark8
@ 2017-11-29 21:07 ` Dmitry A. Kazakov
0 siblings, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2017-11-29 21:07 UTC (permalink / raw)
On 2017-11-29 20:43, Shark8 wrote:
> My rule of thumb is use the most general type as reasonably possible
BTW, that's the principle of weakening preconditions [and strengthening
post-conditions]
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-11-29 21:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-29 19:04 When to use formal discrete type? Victor Porton
2017-11-29 19:43 ` Shark8
2017-11-29 21:07 ` Dmitry A. Kazakov
2017-11-29 21:05 ` Randy Brukardt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox