Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-03

Result: string

16 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

José Mejuto

Hello,

What's the expected output of this code ?

function TheA(): string;
begin
   Result:=Result+'A';
end;
writeln(TheA());

I thought that when the result type is an automated one its value gets initialized... Maybe I'm wrong...

Juha Manninen
to José Mejuto
José Mejuto wrote 11 lines
What's the expected output of this code ? function TheA(): string; begin Result:=Result+'A'; end; writeln(TheA()); I thought that when the result type is an automated one its value gets initialized... Maybe I'm wrong...

Yes you are wrong. It is very illogical because a local string variable is initialized to be empty but the return value is not. Delphi has the same problem.

I once made even a report about it:
http://bugs.freepascal.org/view.php?id=20907

Regards,
Juha

Howard Page-Clark
to Juha Manninen
Juha Manninen wrote 19 lines
On Mon, Mar 4, 2013 at 9:21 PM, José Mejuto wrote: > What's the expected output of this code ? > > function TheA(): string; > begin > Result:=Result+'A'; > end; > > writeln(TheA()); > > I thought that when the result type is an automated one its value gets > initialized... Maybe I'm wrong... Yes you are wrong. It is very illogical because a local string variable is initialized to be empty but the return value is not. Delphi has the same problem. I once made even a report about it: http://bugs.freepascal.org/view.php?id=20907

That report says the issue was assigned to Jonas and fixed in revision 20427 (ver 2.6.1).
I find the current release (2.6.2) initialises a string function result to EmptyStr as you would hope.

Michael Van Canneyt
to Howard Page-Clark
Howard Page-Clark wrote 25 lines
On 04/03/13 8:23, Juha Manninen wrote: > On Mon, Mar 4, 2013 at 9:21 PM, José Mejuto wrote: >> What's the expected output of this code ? >> >> function TheA(): string; >> begin >> Result:=Result+'A'; >> end; >> >> writeln(TheA()); >> >> I thought that when the result type is an automated one its value gets >> initialized... Maybe I'm wrong... > > Yes you are wrong. It is very illogical because a local string > variable is initialized to be empty but the return value is not. > Delphi has the same problem. > > I once made even a report about it: > http://bugs.freepascal.org/view.php?id=20907 That report says the issue was assigned to Jonas and fixed in revision 20427 (ver 2.6.1). I find the current release (2.6.2) initialises a string function result to EmptyStr as you would hope.
{$mode objfpc}
{$h+}
function TheA(): string;
begin
   Result:=Result+'A';
end;
Var
   C : String;
begin
   C:='B';
   C:=TheA;
   C:=TheA;
   Writeln(C);
end.

Prints AA

Michael.

Juha Manninen
to Howard Page-Clark: That report says the issue was assigned to Jonas and fixed in revision 20427 (ver 2.6.1).…
Howard Page-Clark wrote
That report says the issue was assigned to Jonas and fixed in revision 20427 (ver 2.6.1). I find the current release (2.6.2) initialises a string function result to EmptyStr as you would hope.

Ok, that sounds good.
I must have misunderstood what was changed. I thought it was the warning only but apparently the fundamental issue was fixed.

Juha

Flávio Etrusco
to Juha Manninen: Yes you are wrong. It is very illogical because a local string variable is initialized to…
Juha Manninen wrote 23 lines
On Mon, Mar 4, 2013 at 9:21 PM, José Mejuto wrote: > What's the expected output of this code ? > > function TheA(): string; > begin > Result:=Result+'A'; > end; > > writeln(TheA()); > > I thought that when the result type is an automated one its value gets > initialized... Maybe I'm wrong... Yes you are wrong. It is very illogical because a local string variable is initialized to be empty but the return value is not. Delphi has the same problem. I once made even a report about it: http://bugs.freepascal.org/view.php?id=20907 Regards, Juha

Delphi would present this problem "only" in some situations (which is worse), and it didn't even show a warning when the result is a string :-/

-Flávio

José Mejuto
to Michael Van Canneyt: {$mode objfpc} {$h+} function TheA(): string; begin Result:=Result+'A'; end; Var C :…
Michael Van Canneyt wrote
> That report says the issue was assigned to Jonas and fixed in revision > 20427 (ver 2.6.1). > I find the current release (2.6.2) initialises a string function > result to EmptyStr as you would hope.

[...]

Prints AA

Hello,

I'm asking because today I has updated my old 2.7.1 SVN since 8-9 months and a library starts to fail and the "bug" was the Result string which enters the function with a value while it does not happend in the old one.

If Result as string is not initialized a warning should be issued do not ? This happends in your simple test, but I was unable to find it in the library that I'm using.

The code that makes me wonder something is wrong is this one:

-----------------------------------
{$mode objfpc}
{$h+}
procedure TheB(var aTheA: string);
begin
   aTheA:=aTheA+'A';
end;
function TheA(): string;
begin
   //Result:='';
   TheB(Result);
end;
Var
   C : String;
begin
   C:='B';
   C:=TheA;
   C:=TheA;
   Writeln('= "',C,'"');
end.
-----------------------------------
In this situation no warning happends and result is different if I
uncomment the "Result:='';" line.

I think the missing warning is a bug, the missing warning or the non initialization.

Howard Page-Clark
to José Mejuto
José Mejuto wrote 28 lines
The code that makes me wonder something is wrong is this one: ----------------------------------- {$mode objfpc} {$h+} procedure TheB(var aTheA: string); begin aTheA:=aTheA+'A'; end; function TheA(): string; begin //Result:=''; TheB(Result); end; Var C : String; begin C:='B'; C:=TheA; C:=TheA; Writeln('= "',C,'"'); end.

For me (win32, FPC 2.6.2) the output is identical (= "A") whether or not Result in TheA() is initialised manually or not. Perhaps 2.7.1 has a regression here if it differs for you?

Howard

Michael Van Canneyt
to Howard Page-Clark
Howard Page-Clark wrote 34 lines
On 04/03/13 10:33, José Mejuto wrote: > The code that makes me wonder something is wrong is this one: > > ----------------------------------- > {$mode objfpc} > {$h+} > > procedure TheB(var aTheA: string); > > begin > aTheA:=aTheA+'A'; > end; > > function TheA(): string; > > begin > //Result:=''; > TheB(Result); > end; > > Var > C : String; > > begin > C:='B'; > C:=TheA; > C:=TheA; > Writeln('= "',C,'"'); > end. For me (win32, FPC 2.6.2) the output is identical (= "A") whether or not Result in TheA() is initialised manually or not. Perhaps 2.7.1 has a regression here if it differs for you?

It is not a regression. 2.7.1 has the Delphi compatible behaviour.

Michael.

Sven Barth
to Michael Van Canneyt
Michael Van Canneyt wrote 40 lines
On Tue, 5 Mar 2013, Howard Page-Clark wrote: > On 04/03/13 10:33, José Mejuto wrote: > >> The code that makes me wonder something is wrong is this one: >> >> ----------------------------------- >> {$mode objfpc} >> {$h+} >> >> procedure TheB(var aTheA: string); >> >> begin >> aTheA:=aTheA+'A'; >> end; >> >> function TheA(): string; >> >> begin >> //Result:=''; >> TheB(Result); >> end; >> >> Var >> C : String; >> >> begin >> C:='B'; >> C:=TheA; >> C:=TheA; >> Writeln('= "',C,'"'); >> end. > > For me (win32, FPC 2.6.2) the output is identical (= "A") whether or > not Result in TheA() is initialised manually or not. Perhaps 2.7.1 > has a regression here if it differs for you? It is not a regression. 2.7.1 has the Delphi compatible behaviour.

Seems that we should add a remark here:
http://wiki.lazarus.freepascal.org/User_Changes_Trunk

Regards,
Sven

José Mejuto
to Howard Page-Clark: For me (win32, FPC 2.6.2) the output is identical (= "A") whether or not Result in TheA()…
Howard Page-Clark wrote
> The code that makes me wonder something is wrong is this one: >

[...]

Show quoted text 5 lines
For me (win32, FPC 2.6.2) the output is identical (= "A") whether or not Result in TheA() is initialised manually or not. Perhaps 2.7.1 has a regression here if it differs for you?

Hello,

The problem is not the different behavior as it is or will be noted in documentation, the problem is the lack of warning of using a not initialized variable, so it is hard to fix code that works fine in 2.6.x and that could fail with Trunk.

Should I report a bug in Mantis about the warning ?

Jonas Maebe
to José Mejuto
José Mejuto wrote 15 lines
El 05/03/2013 11:23, Howard Page-Clark escribió: > >> The code that makes me wonder something is wrong is this one: >> [...] > > For me (win32, FPC 2.6.2) the output is identical (= "A") whether > or not > Result in TheA() is initialised manually or not. Perhaps 2.7.1 has a > regression here if it differs for you? > The problem is not the different behavior as it is or will be noted in documentation, the problem is the lack of warning of using a not initialized variable,

FPC 2.6.2 does give a warning (code is from http://bugs.freepascal.org/view.php?id=20907#c55064 ):

$ fpc -vw tt7.pp
Free Pascal Compiler version 2.6.2 [2013/02/03] for powerpc Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: Darwin for PowerPC
Compiling tt7.pp
tt7.pp(8,13) Warning: Function result variable does not seem to initialized
Assembling (pipe) tt7.s
Linking tt7
26 lines compiled, 2.6 sec
1 warning(s) issued

so it is hard to fix code that works fine in 2.6.x and that could fail with Trunk.

It could also fail in 2.6.x, just less often.

Jonas

José Mejuto
to Jonas Maebe
Jonas Maebe wrote
FPC 2.6.2 does give a warning (code is from http://bugs.freepascal.org/view.php?id=20907#c55064 ):

[...]

It could also fail in 2.6.x, just less often.

Hello,

2.6.0, 2.6.2 and 2.7.1 trunk produces a warning with that code, but with this one 2.6.0, and 2.7.1 trunk does not generate it (I do not have 2.6.2 to test):

\fpc\svn\bin\i386-win32\fpc test.pp

Free Pascal Compiler version 2.7.1 [2013/03/03] for i386 Copyright (c) 1993-2013 by Florian Klaempfl and others Target OS: Win32 for i386
Compiling test.pp
Linking test.exe
26 lines compiled, 0.1 sec, 29680 bytes code, 1236 bytes data

\fpc\fpc\2.6.0\bin\i386-win32\fpc.exe test.pp

Free Pascal Compiler version 2.6.0 [2011/12/25] for i386 Copyright (c) 1993-2011 by Florian Klaempfl and others Target OS: Win32 for i386
Compiling test.pp
Linking test.exe
26 lines compiled, 0.0 sec , 27264 bytes code, 1692 bytes data

----------------------------------
program test;
{$mode objfpc}
{$h+}
procedure TheB(var aTheA: string);
begin
   aTheA:=aTheA+'A';
end;
function TheA(): string;
begin
   TheB(Result);
end;
Var
   C : String;
begin
   C:='B';
   C:=TheA;
   C:=TheA;
   Writeln('= "',C,'"');
end.
-----------------------------------

And the effect is the same as using a non initialized variable, so it must generate a warning AFAIK. So passing Result as var does not generate a warning.

Jonas Maebe
to José Mejuto
José Mejuto wrote
2.6.0, 2.6.2 and 2.7.1 trunk produces a warning with that code, but with this one 2.6.0, and 2.7.1 trunk does not generate it (I do not have 2.6.2 to test):

That's because you are passing an uninitialized value to a var- parameter. That only generates a hint, because a lot of code uses var- parameters also for parameters that do not yet have to be initialized (because the code predates the existence of the "out" keyword, or simply out of habit).

This is unrelated to string results specifically.

Jonas

José Mejuto
to Jonas Maebe
Jonas Maebe wrote 11 lines
> 2.6.0, 2.6.2 and 2.7.1 trunk produces a warning with that code, but > with this one 2.6.0, and 2.7.1 trunk does not generate it (I do not > have 2.6.2 to test): That's because you are passing an uninitialized value to a var-parameter. That only generates a hint, because a lot of code uses var-parameters also for parameters that do not yet have to be initialized (because the code predates the existence of the "out" keyword, or simply out of habit). This is unrelated to string results specifically.

OK, I see now. I had changed the code to perform the same with integers and yes, it also does not report a warning only a hint which is enought to me.

I'm quite sure that the library that starts this thread does not show a hint, so maybe in some situations it could fail. I'll try to reproduce it in a small code (if there is a problem, of course), meanwhile everything seems to work as expected.

Thank you for your help.

José Mejuto
to Jonas Maebe: That's because you are passing an uninitialized value to a var- parameter. That only…
Jonas Maebe wrote 11 lines
> 2.6.0, 2.6.2 and 2.7.1 trunk produces a warning with that code, but > with this one 2.6.0, and 2.7.1 trunk does not generate it (I do not > have 2.6.2 to test): That's because you are passing an uninitialized value to a var-parameter. That only generates a hint, because a lot of code uses var-parameters also for parameters that do not yet have to be initialized (because the code predates the existence of the "out" keyword, or simply out of habit). This is unrelated to string results specifically.

Hello,

I had found why I was unable to see the hint, because it is not shown due the special contruction of the function.

This code produces a hint for the "Result: String" not initialized, but not for the "Result: integer" one:

------------------------------------------
program test;
{$mode objfpc}
{$h+}
procedure TheB(var aTheA: string);
begin
   aTheA:=aTheA+'A';
end;
function TheA(): string;
begin
   TheB(Result);
end;
procedure TheNumberB(var aTheB: integer);
begin
   aTheB:=aTheB*2;
end;
function TheNumberA: integer;
   procedure HideTheHint;
   begin
     //As it is a procedure the "Result" var is taken
     //from the function scope.
     TheNumberB(Result);
   end;
begin
   HideTheHint;
end;
Var
   C : String;
   N : integer;
begin
   C:='B';
   C:=TheA;
   C:=TheA;
   Writeln('= "',C,'"');
   N:=1;
   N:=TheNumberA;
   N:=TheNumberA;
   Writeln('= "',N,'"');
end.
-------------------------------------------

Yes, it is a bit obfuscated. I do not known if this is a bug in the hint.