Questions regarding force declaration

Started by thaaks, August 29, 2005, 06:43:00 AM

Previous topic - Next topic

thaaks

Hi,

I already found some basic information about forcedeclaration in this thread:
Obsolete Link removed


Open questions are:
Can I have multiple force declarations, say if I have 5 source files can I have 5 declaration sections, one in each file?

I understood that I need to recast Globals. What about constants? Do I need to declare them in the declaration block? What about types? Can I place them anywhere?

A little explanation with sample code in the IDE online manual would also be helpful...

Thanks,
Tommy


Edit: The information in this thread is over 10 years old can be considered obsolete.  Modern editions of PlayBASIC support Explicit  You can enable it anywhere you like. 



kevin

#1
QuoteCan I have multiple force declarations, say if I have 5 source files can I have 5 declaration sections, one in each file?

Yep.  Since you'll need to declare each scope independently..


QuoteI understood that I need to recast Globals. What about constants? Do I need to declare them in the declaration block? What about types? Can I place them anywhere?

PB is top down by design,  So declarations must proceed their usage.   ForceDeclaration works differently than Option Explicit in VB for example, as it requires you to register all "KeyWords" before their use.  This not only means Variables Names but Type Names and Even Fields need to be registered.  Which is a tad overly strict, but that's the way the current implementation works.
.  

With Globals/Constants,  You can register their keywords (names) if you like, but it's not actually required.


QuoteA little explanation with sample code in the IDE online manual would also be helpful...

 it's currently undocumented for a reason..  The reason being is mentioned above :)
 






Forcedeclaration

; _Every_ variable/type name and field name must be declared prior to use

; Do NOT put comments between he declartion markers  it's throw the parser
; into and infinite loop (known bug)

Declaration
    MyVariablesInHere
    lp
    Rect,X1,Y1,X2,Y2
EndDeclaration

; declare a type
Type Rect
X1,Y1,X2,Y2
EndType

; create an array  (array name keywords don't require to be pre-declared
Dim Boxes(100) as Rect

; Dim an int array (arrays the dim will declare
Dim Stuff(10)


; Constants Can be declared as normal,  although if you wanted you could
; list thier keywords about. but it's not actually required
Constant Gravity#=9.8

; Same as globals
Global Speed#=100


; Wack some data in this variable
MyVariablesInHere=2

;  
Print MyVariablesInHere
Print Speed#


Print Add(1000,999)


For lp=0 to 10
  Stuff(lp)=rnd(100)
  Boxes(lp).x1=100
  Boxes(lp).y1=200
  Boxes(lp).x2=Boxes(lp).x1+100
  Boxes(lp).y2=Boxes(lp).y1+200
  print Stuff(lp)
  print Boxes(lp).x1
 print Boxes(lp).y1
 print Boxes(lp).x2
 print Boxes(lp).y2
next


; You can have declare block any where, since you need to declare your variables
; within your functions also
Declaration
   I_Cant_declare_other_Things_here
EndDeclaration



sync
waitkey




; The input params are pre-declared
Function Add(A,B)
; Declare some variable keywords
  Declaration
     Temp
     Result
  EndDeclaration
  Temp=A+B

  Result=Temp*2

  Print "Global Speed Value"+Str$(speed#)
  Print "Global Speed Value"+Str$(speed#)

; return variables need to be declared
EndFunction Result


thaaks

Thanks for the good explanation! Now I know why the compiler didn't like my first tries  :rolleyes:

Is the behavior (need of declaring types and fields) going to stay or do you plan to change it in some future release? I would love to use forced declaration as soon as possible. I could right now but if you say it'll change in the next release I would wait a bit...

I think forced declaration is a pretty good mechanism to avoid one of the big troublemakers in Basic: having a typo in a variable name and automatically declaring a new global variable with value 0. Took me an hour two weeks ago to find such a bug  :angry:

Thanks for having such a feature in PlayBasic!

kevin

While the way it's currently implemented does the job i guess, in the past we've discussed  moving declaration more towards a more VB styled approach, where you'll activate/deactivate the mode and then use DIM to declare and define a variables.  It'll prolly blend the two ideas.

kevin

Update

 I had always intended upon removing Force declaration and the declaration brackets from PB.   I just didn't like how it was implemented.  However while updating that today, i've been able tweak it up to much more pleasing to the user.  

 Currently there are two ways to declare variables when declaration is being enforced.   The first is through a delcaration/enddeclaration list.  This is prolly the quickest option if you wish to declare many variables.   The other option is through DIM.

old way


Declaration
   A,B
End Declaration


or.


  Dim A as integer
  Dim B as float


Note  PB1.18  does NOT support post fixes on float and string declaration, via the DIM statement.  This short coming will be addressed later)

You can also toggle the declaration mode ON and OFF.  

I.e

Forcedeclaration ON
Forcedeclaration OFF

And finally i've added an ALIAS for the Force declaration statement.  So  now  Explicit and ForceDelcaration are interchangeable


Available in PB1.18

thaaks

Funny you currently work on this. The last days I thought about this issue myself and had the idea of a much easier approach (for a PB user  ;) ).

You already have a two pass compiler, right? So you should be able to find out which variables/types/globals/constants/arrays/functions/Psubs are declared/used within the whole project's source code in the first pass, right?

So you would just need to store those in pass one.

If forced declaration is activated the second pass would need to raise an error for any identifier that is not declared via a Global, Constant, Dim, Type Function, PSub or Local statement.

That would ease the typing for PB users cause it would imply only the oneliner to switch on forced declaration.
No declaration blocks, no Dim required.

Of course it might be true that I'm completely off track and forgot thousands of complicated issues  :rolleyes:

Anyway, any work on getting a usable "force declaration" feature in PB is highly appreciated! At least by me  :D

Cheers,
Tommy

kevin

#6
Well, the work on this is jus a by product of work i'm doing on types.  Where i need to introduce some new data types.  And needed a way to declare them.

 The updated implementation is a lot more user friendly than the previous one.  While I haven't removed the existing solution, partly for backward compat reasons and personally I tend to mass declaration easier between declaration statements  

 Now when declaration is being enforced (which you can now turn on/off ant any time)  PB now allows variables to be defined (created) within any of the various declaration methods (Dim, Global,Local,Static).  But they have to be pre-declared.  

 Dim support for variables is still pretty green, but ya get that.
 


Example for PlayBASIC v1.18

PlayBASIC Code: [Select]
  ForceDeclaration 

global Hello=42
local Santa

Type Pos
x
y
z
EndType


Dim w as integer
Dim h as integer
Dim tim1 as integer
Dim tim2 as integer
Dim ypoint as integer
Dim xpoint as integer
Dim c as integer
Dim c2 as integer
Dim basec as integer
dim Name as string
dim F as float


w=getscreenwidth()
h=getscreenheight()

w=800
h=600
w=320
h=240
openscreen w,h,32,1
rendertoscreen


; turn it off, and alow dynamic creation again
Explicit Off
; ForceDeclaration Off

zzxx=1

; Explicit On
ForceDeclaration on



Dim Stuff as Pos

basec=rgb(255,0,255)
Do
cls 0

Stuff.x=11
Stuff.y=12
Stuff.z=13


rendertoscreen
dot 0,ypoint

tim2=timer()
c=basec
lockbuffer
For ypoint=0 To h-1
c2=c
For xpoint=0 To w-1
fastdot xpoint,ypoint,c2
Next
c=c+xpoint+1
Next
unlockbuffer
tim2=timer()-tim2
basec=basec+w

; show the time of DOTC and FASTDOT
print fps()
print "MS"+str$(tim1)
print "MS"+str$(tim2)
print "Fill Rate:"+str$(Float(w*h)/tim2)
print stuff.x
print stuff.y
print stuff.z
sync
loop





thaaks

If your code example is already working then it looks very usable to me.
Much better than the current approach  :D

What about Arrays of base types (int, float, string) and Arrays of user defined types? That should work similar, right?

What about case sensitive names? Just for us old 'Insert your favorite case sensitive programming language here' users?  :rolleyes:
But it's not that important, just a nice to have.

Does the Explicit mode make a difference between a declared variable FOOBAR and a used but undeclared variable foobar?

Can't wait for PB 1.18  ;)  And you just released 1.17  :(

Cheers,
Tommy

kevin

The above is already implemented and seems to be working fines.   Regarding arrays, their already explicitly defined.

As for implementing  'case sensitively',  well the jury is still out on that one.

 I'll upload a new beta in a little while.

thaaks

#9
Woohoo, already saw the download link ! That's great!  :D

Will give it a test run tonight or wednesday night (don't forget: tuesday is valentine's day - I need to hug my lovely wife  ;) )!

BTW: Kevin, do you ever, ever sleep?
Or are you following Steve Pavlina's polyphasic sleep system?
Link here: (login required)

Wondering,  :rolleyes:
Tommy