It is currently Wed Jan 27, 2021 7:49 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: [Done] Parameter and protected variable with equal names
PostPosted: Fri Jul 03, 2020 12:44 pm 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3967
Location: Berlin, Germany
Hi all,

when a procedure parameter has the same name as a protected variable in that procedure, different things happen, depending on the type of the parameter/variable (tested with PB 5.72 LTS x64 on Windows):
  • both are standard variables (e.g. integers): compiler error
  • both are arrays: no error, the protected array is just ignored
  • both are lists: runtime error outside of the procedure
  • both are maps: runtime error outside of the procedure
Other combinations not tested.

Shouldn't PureBasic behave consistently in all cases mentioned above?

Code:
EnableExplicit

Procedure DemoA (x.i)
   Protected x.i   ; -> Compiler error
   
EndProcedure

Define a.i
DemoA(a)

Code:
EnableExplicit

Procedure DemoB (Array x.i(1))
   Protected Dim x.i(4)   ; -> NO compiler error
   
   x(1) = 1
   x(2) = 2
EndProcedure

Define i.i
Dim b.i(4)
DemoB(b())

For i = 0 To 4     ; -> NO runtime error
   Debug b(i)
Next

Code:
EnableExplicit

Procedure DemoC (List x.i())
   Protected NewList x.i()   ; -> NO compiler error
   
   AddElement(x()) : x() = 1
   AddElement(x()) : x() = 2
EndProcedure

NewList c.i()
DemoC(c())

ForEach c()     ; -> Runtime error
   Debug c()
Next

Code:
EnableExplicit

Procedure DemoD (Map x.i())
   Protected NewMap x.i()   ; -> NO compiler error
   
   x("a") = 1
   x("b") = 2
EndProcedure

NewMap d.i()
DemoD(d())

ForEach d()     ; -> Runtime error
   Debug d()
Next

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Fri Jul 03, 2020 3:14 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 1145
I think only the first example is correct. Param variables are like Protected. With Array, Lists and Maps it is different, because they pass the reference. However, the error detection is wrong.

But I wouldn't classify that as a bad mistake, because it depends on you as a programmer that it does not happen.

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Fri Jul 03, 2020 3:23 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 806
Looks correct to me.

The first should give a error since its the same.
In the second example the array gets just reinitialized so no error.
In the other examples the old list/map gets overwritten.


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Sat Jul 04, 2020 12:51 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2820
Location: Germany
Its a Bug,
please move to bug report

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Sun Jul 05, 2020 1:28 pm 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3967
Location: Berlin, Germany
Josh wrote:
With Array, Lists and Maps it is different, because they pass the reference.
Technically speaking, this is correct. However, for me as a programmer, the problem is the same: I accidentally gave a parameter and a Protected variable the same name, which can lead to problems.

Josh wrote:
But I wouldn't classify that as a bad mistake, because it depends on you as a programmer that it does not happen.
Yes, but why should I as a programmer take care of this problem if the compiler could do it? For me it is a question of convenience and reliability. To err is human, and the compiler can find such problems more reliably than a human. For the same reasons PureBasic also checks e.g. the compliance of array bounds.

And consistent behaviour is the least we can expect. The most sensible behaviour in all mentioned cases would be to trigger a compiler error, as it happens in the first example code that I posted. This behaviour should then be documented. The current behavior in these cases is so strange that it is hardly possible to document it at all.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Sun Jul 05, 2020 1:46 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5818
Location: Germany
The behavior should be the same in all cases. Moved to bugs.

_________________
quidquid Latine dictum sit altum videtur


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Sun Jul 05, 2020 3:00 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6287
Location: Wales, UK
Why use the same names? Surely that's not the best idea in the world......

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Sun Jul 05, 2020 3:12 pm 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3967
Location: Berlin, Germany
freak wrote:
Moved to bugs.

Thank you, freak!

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Mon Sep 14, 2020 2:31 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14181
Location: France
Fixed.


Top
 Profile  
Reply with quote  
 Post subject: Re: Parameter and protected variable with equal names
PostPosted: Tue Sep 22, 2020 5:29 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 725
Location: Canada
@Little John :
Bravo.
We can always use better logic, more clarity, and, of course, a little more help from our friendly compiler.


@IdeasVacuum :

IdeasVacuum wrote:
Why use the same names? Surely that's not the best idea in the world......

Ah ! But surely "If it sounds simple, you have not grasped the complexity." :D

_________________
"That's not a bug..." said the programmer. "...It's a feature! "
"Oh! I see..." replied the blind man.


Top
 Profile  
Reply with quote  
 Post subject: Re: [Done] Parameter and protected variable with equal names
PostPosted: Thu Oct 08, 2020 6:28 am 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3967
Location: Berlin, Germany
Fix confirmed with PB 5.73 beta 1 (on Windows).
Thank you, Fred!

However, there is a problem with the concerning German error messages.

The English error messages are clear and consistent:
Error message when running DemoA() wrote:
Line 4: Local variable already declared: x.
Error message when running DemoB() wrote:
Line 4: Array already declared: x().
Error message when running DemoC() wrote:
Line 4: List already declared: x().
Error message when running DemoD() wrote:
Line 4: Map already declared: x().


And here are the German error messages:
Error message when running DemoA() wrote:
Zeile 4: Lokale Variable bereits deklariert: x.
Error message when running DemoB() wrote:
Zeile 4: Array-Parameter bereits deklariert: x().
Error message when running DemoC() wrote:
Zeile 4: Diese LinkedList existiert bereits: x.
Error message when running DemoD() wrote:
Zeile 4: Modul 'public' Konstante '#x' ist bereits im globalen Gültigkeitsbereich deklariert.

The German error message when running DemoD() is wrong and confusing.
The English error messages are fine. So just translate them literally into German.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: [Done] Parameter and protected variable with equal names
PostPosted: Thu Oct 08, 2020 12:26 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14181
Location: France
You're right, this is because I reused some message ID. It should updated in the catalog file (if you can do it, I will put it back in official catalog)


Top
 Profile  
Reply with quote  
 Post subject: Re: [Done] Parameter and protected variable with equal names
PostPosted: Thu Oct 08, 2020 1:16 pm 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3967
Location: Berlin, Germany
Yes, of course.
I'll do it when I'm home in a few hours.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: [Done] Parameter and protected variable with equal names
PostPosted: Thu Oct 08, 2020 7:20 pm 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3967
Location: Berlin, Germany
I changed 3 lines in the [ErrorCodes] section of the German "Compiler.catalog" file for PB 5.73 beta 1.

The new lines read like this:
Code:
139 = Linked List bereits deklariert: %s().
217 = Array bereits deklariert: %s().
288 = Map bereits deklariert: %s().

You can also download the complete modified German "Compiler.catalog" file from here:
https://www.dropbox.com/s/dk4n0s8tgxaow ... g.zip?dl=1
With that modified file, I get exactly the error messages that I expect when running the test code snippets from my first post in this thread.

However, of course I personally do not know whether the modified error messages will also be used in another context. And if this is the case, whether they are now correct in the other context.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: [Done] Parameter and protected variable with equal names
PostPosted: Fri Oct 09, 2020 10:15 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14181
Location: France
It's patched, thank you.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye