Result: string

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Result: string

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...

--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Juha Manninen
On Mon, Mar 4, 2013 at 9:21 PM, José Mejuto <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Howard Page-Clark
On 04/03/13 8:23, Juha Manninen wrote:

> On Mon, Mar 4, 2013 at 9:21 PM, José Mejuto <[hidden email]> 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.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Michael Van Canneyt


On Mon, 4 Mar 2013, Howard Page-Clark wrote:

> On 04/03/13 8:23, Juha Manninen wrote:
>> On Mon, Mar 4, 2013 at 9:21 PM, José Mejuto <[hidden email]> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Juha Manninen
In reply to this post by Howard Page-Clark
On Mon, Mar 4, 2013 at 10:51 PM, Howard Page-Clark <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

etrusco
In reply to this post by Juha Manninen
On Mon, Mar 4, 2013 at 5:23 PM, Juha Manninen <[hidden email]> wrote:

> On Mon, Mar 4, 2013 at 9:21 PM, José Mejuto <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

José Mejuto
In reply to this post by Michael Van Canneyt
El 04/03/2013 22:16, Michael Van Canneyt escribió:

>> 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.

--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Howard Page-Clark
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?

Howard

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Michael Van Canneyt


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.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Sven Barth-2
Am 05.03.2013 11:27, schrieb Michael Van Canneyt:

>
>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

José Mejuto
In reply to this post by Howard Page-Clark
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?
>

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 ?

--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Jonas Maebe-2

On 05 Mar 2013, at 12:34, José Mejuto wrote:

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

José Mejuto
El 06/03/2013 10:40, Jonas Maebe escribió:

> 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.

--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

Jonas Maebe-2

On 06 Mar 2013, at 13:37, 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

José Mejuto
El 06/03/2013 13:47, Jonas Maebe escribió:

>> 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.


--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Result: string

José Mejuto
In reply to this post by Jonas Maebe-2
El 06/03/2013 13:47, Jonas Maebe escribió:

>> 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.

--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal