|
Post by ptitjoz on Mar 10, 2018 9:14:02 GMT 1
Hello
I try to transform a FreePascal program into Bacon but I have a problem. Variables of the same name do not seem to work in Global and Local
here un little example :
In FreePascal
program LocalGlobal;
var i: integer; //scope global
procedure testlocal(); var i: integer; // scope local begin i:=10; writeln('local ',i); end;
//Main ----------------- begin i:=100; writeln('global ',i); testlocal(); writeln('global ',i); readln; end. The result
In bacon
OPTION EXPLICIT TRUE GLOBAL i TYPE NUMBER
SUB SubLocal LOCAL i TYPE NUMBER i=10 PRINT "local ";i END SUB
'Main i=100 PRINT "global ",i sublocal() PRINT "global ",i END
The result
Regards
|
|
|
Post by vovchik on Mar 10, 2018 11:28:02 GMT 1
Dear ptitjoz,
I think BaCon is working very properly here, and EXPLICIT tells us. GLOBAL means the var can be seen everywhere - in SUBs and FUNCTIONs too - so a declaration using a var name that has been made visible to every procedure cannot be redefined in a SUB. Peter will give us the authoritative answer...
With kind regards, vovchik
|
|
|
Post by Pjot on Mar 10, 2018 12:29:50 GMT 1
Hi ptitjoz, BaCon does not support variable shadowing. This is to prevent errors where variables with a global scope (this is the default in BaCon) mix up with variables with local scope. BR Peter
|
|
|
Post by ptitjoz on Mar 11, 2018 16:11:00 GMT 1
Hello, Peter and vovchik
Thank you for your answers.
I thought that declaring declare a variable in Local and / or Global avoided problems This allows several developers to develop their own routines without taking care of others.
For example if I use Hug.bac there are variables that I can not use anymore. As I have not written hug.bac (or others) it is sometimes embarrassing. The solution is perhaps for each developer prefixed its variables by a code? Regards
|
|
|
Post by vovchik on Mar 11, 2018 17:22:06 GMT 1
Dear ptitjoz,
I had thought about that problem too, but your solution is a good one. I think I will implement it in future. Thanks for the idea. It is simple and easy.
With kind regards, vovchik
|
|
|
Post by Pjot on Mar 12, 2018 18:43:52 GMT 1
I thought that declaring declare a variable in Local and / or Global avoided problems This allows several developers to develop their own routines without taking care of others. Using the LOCAL statement consequently within functions actually will prevent that you run into errors. Mixing globals and locals will 'lead to confusion', as the Wikipedia article mentions. For example if I use Hug.bac there are variables that I can not use anymore. As I have not written hug.bac (or others) it is sometimes embarrassing. The solution is perhaps for each developer prefixed its variables by a code? Best practice is of course to avoid global variables altogether. The HUG context uses global variables but all of them are prefixed by 'hug_' or 'HUG_'. Feel free to improve the implementation and propose a better version! Best regards Peter
|
|
|
Post by ptitjoz on Mar 13, 2018 9:49:07 GMT 1
Hello Peter Thank you for all these explanations.
Regarding hug when I put the option EXPLICIT I have errors. For example If I correct line 402 in my version of hug.bac, other errors appear on other lines ...
Is it possible for a new version to include the declaration of variables that have been forgotten?
Regards
Joz
|
|
|
Post by vovchik on Mar 13, 2018 18:01:24 GMT 1
Dear Joz, Peter and I have already discussed this and an official fix will be out soon, I think. In the interim, you can use my fixed version. As I recall (I did this more than a week ago), three variables were involved. With kind regards, vovchik Attachments:hug.bac.tar.gz (17.69 KB)
|
|
|
Post by Pjot on Mar 13, 2018 18:47:52 GMT 1
Dear ptitjoz,
Alternatively, you can put OPTION EXPLICIT after the line which includes hug.bac.
BR Peter
|
|
|
Post by ptitjoz on Mar 14, 2018 8:19:42 GMT 1
Hello everyone
Thanks for modified hug.bac and thanks for the different explanations. I thought the OPTION EXPLICIT place did not really matter. I did not think the code analysis was sequential.
Regards
|
|