Re: Default values of functions
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Wed, 18 Mar 2009 01:39:37 -0500
Paul E. Schoen wrote:
"Ikke" <ikke@xxxxxxx> wrote in message news:Xns9BD1EDA38B672ikkehierbe@xxxxxxxxxxxxxxxx"BRoberts" <berdontemail@xxxxxxxxxx> wrote in
news:48c3b$49bef597$45c49f8e$32362@xxxxxxxxxxxxxxxxx:
If "some statements" fail then an exception is raised and the returnI'm afraid I don't completely understand what you mean. Here's the actual
value of the function is moot. Hence the message. There is no path
through the function, as you've shown it, in which the function result
will be used by the caller and in which the initial assignment to
result is not replaced.
code I dug up at work:
--- code ---
function AllInvoicesBooked: Boolean;
Var
query : TGCQuery;
begin
result := false;
query := TGCQuery.Create(nil);
try
query.SQL.Clear;
query.SQL.Add('SELECT 1 FROM INVOICES WHERE COMPANY = :Company');
query.ParamByName('Company').AsInteger := intFirm;
query.Open;
result := query.IsEmpty;
finally
query.Free;
end;
end;
--- /code ---
Upon compiling, the compiler issues the hint "Value assigned to
�AllInvoicesBooked� never used." .
As I see it, there are three possibilities:
- the query succeeds, and is empty: the function returns true.
- the query succeeds, and is not empty: the function returns false;
- the query fails (an exception occurs): the function returns false (the
initial given value)
Suppose you don't initialise the function, what would be the value of the
function in the third scenario?
I thought that an error in any of the query methods would invoke the default error handler,
The what?
and then execution would continue to the next statement until the last was executed, at which point the finally section would free the query object.
Kind of makes try-finally pointless, doesn't it? I mean, if execution continues at the very next statement anyway, what would a "finally" block really do?
So, unless the IsEmpty property raises an exception, the return value would be that value.
I suppose that could happen if the query.create method failed and query would be nil. So it might be good to check for a nil value before proceeding with the try statements.
But the Delphi Help says that any exception in the try statement list causes control to pass to the finally section, which should handle any exception.
Surely the help doesn't say that a "finally" section should handle exceptions, does it? If it does, it's flat-out wrong.
After the finally section has finished, the exception is re-raised, but is lost if not handled before the function exits.
So I agree that the return value of this function could be undefined if the assignment is ignored by the compiler.
Where do you get that interpretation from the help you just read?
If an exception escapes from a function, then that function has *no* return value. It's not that the return value is undefined -- using words like "defined" or "undefined" is impossible when referring to something that doesn't exist at all.
And the calling code will not know if the return value correctly indicates the result of the query.IsEmpty property.
But that's OK because the calling code will never see the continuation of the instruction that was calling the function in the first place. If the calling code called the function inside a "try" section, then execution, upon leaving the called function, will go to the "finally" section of the caller, if there is one, or to the "except" section of the caller, if there is one with the appropriate exception type. If the call was not made in a "try" section, then execution will skip over the caller altogether and go to the caller's caller to look for a "finally" or "except" block. In particular, the caller's code that does stuff with the function's return value is skipped. So it doesn't matter that the exception-raising function has no return value because the code that wanted to use the return value never executes.
So the compiler hint is completely correct. There is no code path that could do the first result assignment without doing any subsequent result assignments, but still leave the function in such a way that the result could be used.
--
Rob
.
- Follow-Ups:
- Re: Default values of functions
- From: Paul E. Schoen
- Re: Default values of functions
- References:
- Default values of functions
- From: Ikke
- Re: Default values of functions
- From: BRoberts
- Re: Default values of functions
- From: Ikke
- Re: Default values of functions
- From: Paul E. Schoen
- Default values of functions
- Prev by Date: Re: Default values of functions
- Next by Date: Re: Default values of functions
- Previous by thread: Re: Default values of functions
- Next by thread: Re: Default values of functions
- Index(es):
Relevant Pages
|