On 13 Aug 2003 at 23:44, Frank Heckenbach wrote:
Prof. A Olowofoyeku (The African Chief) wrote:
On 12 Aug 2003 at 21:54, Frank Heckenbach wrote:
[...]
"Val( S: String; var V; var Code: Integer)"
"... If the string is somehow invalid, the index of the offending character is stored in Code;
The question is if V is (guaranteed to be) zero then.
I wouldn't have thought so. The value of V might well be undefined. My understanding (which of course may be wrong) is that nothing is done with V unless the procedure call succeeds. So whatever was in V before the procedure call would still be there afterwards,
This would seem reasonable, but apparently not what BP does. This gives `0 3'.
program Foo; var x, y: Integer; begin x := 10; y := 20; Val ('34c', x, y); WriteLn (x, ' ', y) end.
It does so here as well. So does FPC. However, Delphi (from v3.0 to 7.0) produces "34 3".
but I do not think that even this is guaranteed.
It might still be undefined in BP. (My printed manual doesn't state it, either.) Then again, many things that are undocumented are relied upon by BP programmers and required for "full" compatibility. I don't know if this will be one of them.
Perhaps. I don't think it will harm any BP programmer if GPC guarantees it to always be zero. In a small program like the above, you may get zero all the time - but what if it was in the middle of a 1,000-line program?
Mirsad Todorovac wrote:
The other side of the story is that Val could be used more flexible, i.e. for parsing if "best effort" has been done, and string has been converted-up to the first invalid character/digit.
We could do that if we decide to interpret it as undefined.
However, since `Val' is a BP function, anyway, I'd tend to change GPC's behaviour and set the number to 0. (In the case where you want to ignore the trailing garbage, it's easy to pass the prefix to `Val' again.)
What Mirsad suggests seems to be what Delphi does (i.e., stop where the error starts, and return what was possible to convert before the error - but again the Delphi help file does not document this, so I am not sure that one should rely on this behaviour).
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.bigfoot.com/~african_chief/