Author Topic: Option Explicit doesn't work with SHARED?  (Read 3954 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Option Explicit doesn't work with SHARED?
« Reply #15 on: August 13, 2019, 04:27:14 pm »
About this
Quote
using OPTION _EXPLICIT forces you to DIM a hell of allot more things (everything) but that in turn forces you to think allot more about types the variable may declared.
I think that stopping the compilation  this OPTION _EXPLICIT works with a Pascal spirit and not with a BASIC spirit.
IMHO in Pascal the absence of declaration is a syntax's error. So you first think, then declare and after type code.

in the end
this

Code: [Select]
IncreaseT
PrintT
END

SUB IncreaseT
SHARED t  AS INTEGER
t = t +1
END SUB

SUB PrintT
SHARED t AS INTEGER
PRINT  t
END SUB

is the implicit form of this one that follows

Code: [Select]
t = 0
IncreaseT
PrintT
END

SUB IncreaseT
SHARED t  AS INTEGER
t = t +1
END SUB

SUB PrintT
SHARED t AS INTEGER
PRINT  t
END SUB

so we can understand why OPTION _EXPLICIT claims declaration of variables, that are SHARED into SUB/FUNCTION, into main.
But in QB SHARED is the same? If so for backcompatibility with QB code OPTION _EXPLICIT must warn and not halt.

Thanks to read
« Last Edit: August 14, 2019, 07:51:49 am by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Option Explicit doesn't work with SHARED?
« Reply #16 on: August 14, 2019, 06:35:51 am »
Here's a tweak to QB64.bas which you guys might enjoy:

Code: QB64: [Select]
  1.                     IF optionexplicit THEN
  2.                         _CLIPBOARD$ = "DIM " + x$ + " AS " + symbol2fulltypename$(typ$)
  3.                         Give_Error "Variable '" + x$ + "' (" + symbol2fulltypename$(typ$) + ") not defined"
  4.                         EXIT FUNCTION
  5.                     END IF

SEARCH for IF optionexplicit, and then before any call to the error handler, insert a _CLIPBOARD statement, like the one above.   When QB64's IDE complains "Variable foo (INTEGER) not defined on Line ###", your clipboard with hold a statement of "DIM foo AS INTEGER", so all you have to do is paste it into your code inside the proper spot.  (Top of main code, inside the sub, wherever...)

It's a must learn trick, if you're going to be updating old code so it'll work properly without any OPTION _EXPLICIT error messages popping up.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Option Explicit doesn't work with SHARED?
« Reply #17 on: August 14, 2019, 08:18:30 am »
What an interesting idea! Thanks Steve