comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Ada2012 : When to use expression functions rather than function bodies?
Date: Fri, 16 Dec 2011 19:21:45 -0600
Date: 2011-12-16T19:21:45-06:00	[thread overview]
Message-ID: <jcgqrb$cdv$1@munin.nbi.dk> (raw)
In-Reply-To: 12acbf57-01fc-43dd-8881-d39c2a63146b@q9g2000yqe.googlegroups.com

"Adam Beneschan" <adam@irvine.com> wrote in message 
news:12acbf57-01fc-43dd-8881-d39c2a63146b@q9g2000yqe.googlegroups.com...
...
>Apparently the ARG thought this was a good argument.  I'm not
>endorsing it personally -- I don't have a particular opinion -- but it
>appears sensible on its face.

One of the things we learned when thinking about Preconditions and the like 
is that there is such a thing as too much encapsulation. The entire point of 
preconditions is that they be understandable to the client, so the client 
(caller) knows what they must ensure before calling the routine.

You could write all of your preconditions like:

     procedure Do_It (A, B : in out Integer)
         with Pre => Do_It_Precondition (A, B);

but no one would have any idea what the precondition is. After all, an 
important part of the point of preconditions is to change what currently is 
specified in English in the comments in a more formal way that can be 
checked (statically and dynamically).

So it is best to use as little encapsulation as possible in preconditions 
and the like, breaking them down to the primitive operations of the type(s) 
involved.

But preconditions can get very long, and the need to simplify them for 
readability surely exists. Thus expression functions provide a middle 
ground. They're also handy for simple accessor functions (which can give the 
effect of read-only components of a private type).

There also is one more fact: an expression function can almost always be 
inlined (beware of recursion, though), and that can be done automatically 
when it makes sense (no need for pragmas or body dependencies -- inlining is 
something the compiler should be deciding upon anyway, it is stupid for 
users to have to declare something that should always be done if it makes 
sense -- and not be done when it doesn't make sense).

                                        Randy.





  parent reply	other threads:[~2011-12-17  1:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-16 12:25 Ada2012 : When to use expression functions rather than function bodies? Martin
2011-12-16 13:24 ` Dmitry A. Kazakov
2011-12-17  1:03   ` Randy Brukardt
2011-12-16 18:03 ` Adam Beneschan
2011-12-16 20:36   ` Martin Dowie
2011-12-16 21:34     ` Adam Beneschan
2011-12-16 23:08       ` Adam Beneschan
2011-12-17 12:26   ` georg bauhaus
2011-12-16 22:01 ` Jeffrey Carter
2011-12-16 22:52   ` Adam Beneschan
2011-12-16 23:09     ` Adam Beneschan
2011-12-17  1:21     ` Randy Brukardt [this message]
2011-12-17 12:45       ` georg bauhaus
2011-12-17 13:11         ` Dmitry A. Kazakov
2011-12-19 23:34           ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox