Type and Scope Confusion - Beginner

New to FreeBASIC? Post your questions here.
Red2
Posts: 18
Joined: Mar 29, 2018 22:21

Type and Scope Confusion - Beginner

Post by Red2 »

Question/Help regarding scope?
I'm new to FreeBasic and very confused regarding values in a TYPE going away within a loop when the loop contains more than 1 WITH...ENDWITH.

Thank you for any clarification here!!!

'' QUESTION: What is the scope issue for WITH...END WITH?
'' Two separate WITH...END WITH blocks show TYPE's values set to 1 become 0 with each loop iteration.
'' However a single large encompassing WITH...END WITH maintain values set to 1.
'' To see both cases simply change WITH...END WITH block commenting.


'' File Requirements:
'' 1) A folder "C:\MyFolder\" exists
'' 2) It contains three files:
'' "Form_Answers.TXT"
'' "By State.CSV"
'' "Items List.CSV"


#define CRLF chr(13, 10)
type RequiredFiles
Form_Answers as byte = 0
By_State as byte = 0
Items_List as byte = 0
end type '' RequiredFiles

dim UpperFileName as string = ""
dim MissingFiles as string = ""
dim FileSpec as string

'' with RequiredFiles '' XXXXXXXXXXXXXX -- For a SINGLE block

dim As string FileName = Dir( "C:\MyFolder\*.*" )
do while len( FileName ) > 0
UpperFileName = ucase( FileName )
with RequiredFiles '' XXXXXXXXXXXXXX -- For 2 separate blocks
if ( UpperFileName = "FORM_ANSWERS.TXT" ) then
.Form_Answers = 1
elseif ( UpperFileName = "BY_STATE.CSV" ) then
.By_State = 1
elseif ( UpperFileName = "ITEMS_LIST.CSV" ) then
.Items_List = 1
endif

'' SCOPE: Any 1 values may become 0 with next loop iteration:
print FileName + " " & .Form_Answers & ", " & .By_State & ", " & .Items_List
end with '' RequiredFiles '' XXXXXXXXXXXXXX -- For 2 separate blocks

sleep
FileName = Dir()
loop

with RequiredFiles '' XXXXXXXXXXXXXX -- For 2 separate blocks
if ( .Form_Answers = 0 ) then
MissingFiles += " Form_Answers.TXT NOT found." + CRLF
endif
if ( .By_State = 0 ) then
MissingFiles += " By_State.CSV NOT found." + CRLF
endif
if ( .Items_List = 0 ) then
MissingFiles += " Items_List.CSV NOT found." + CRLF
endif

print CRLF + "Test results:"
print "Final values: " & .Form_Answers & ", " & .By_State & ", " & .Items_List

end with '' RequiredFiles '' Leave this in place for BOTH instances
print MissingFiles
print "SCOPE ISSUE: Results when 2 separate blocks are used say the files are NOT found."
print " HOWEVER: With a single encompassing WITH...END WITH the files are found."

sleep
end
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Type and Scope Confusion - Beginner

Post by D.J.Peters »

May be you found a bug !
"WITH RequiredFiles" should not compile at all
RequiredFiles are a TYPE/STRUCT descriptor
but not a reference to any memory.

There isn't a DIM RequiredFiles as RequiredFiles

Joshy
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Type and Scope Confusion - Beginner

Post by paul doe »

D.J.Peters wrote:...
"WITH RequiredFiles" should not compile at all
RequiredFiles are a TYPE/STRUCT descriptor
but not a reference to any memory.
And indeed it doesn't, at least in the latest versions (1.05 and 1.06).

@Red2: what compiler version are you using? Perhaps yours is an old one...
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Type and Scope Confusion - Beginner

Post by fxm »

When a "UDT" has a constructor (for example induced by a field initializer), the term "UDT" can not only represent the typename, but also a created temporary instance (short cut of "UDT()").

When used several time in [With UDT ... End With] blocks, each "UDT" represents therefore a temporary local variable for its own scope [With UDT ... End With].

So no bug.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Type and Scope Confusion - Beginner

Post by D.J.Peters »

@fxm if a name of a UDT is a instance in memory than it's a bug or this "feature" isn't BASIC anymore.
I talk about a BASIC UDT not class with constructor's.

Joshy
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Type and Scope Confusion - Beginner

Post by fxm »

The only bug could be that the parser accepts "UDT" as a shortcut of "UDT()" ("UDT()" being the right syntax):

Code: Select all

With UDT()
.....
End With
is fully legitimate.

A too smart parser, which interprets depending on context?
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Type and Scope Confusion - Beginner

Post by paul doe »

fxm wrote:The only bug could be that the parser accepts "UDT" as a shortcut of "UDT()" ("UDT()" being the right syntax):

Code: Select all

With UDT()
.....
End With
is fully legitimate.
Mmm... I don't know. What would be the point of that construct?
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Type and Scope Confusion - Beginner

Post by fxm »

"UDT()" is the constructor call of the UDT.

Another possible syntax for this case:
Type<UDT>()
("Type<UDT>" for the shortcut)

Since dkl left, I feel a bit lonely in this area of syntax!
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Type and Scope Confusion - Beginner

Post by fxm »

See changelog.txt:
Version 1.01.0
.....
{added]
.....
- WITH compounds now also accept type<UDT>(...) and UDT(...) expressions
.....
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Type and Scope Confusion - Beginner

Post by fxm »

For me, the only remaining question is to allow or not these shortcuts "UDT" and "Type<UDT>" (without empty parentheses) for the creation of temporary instances.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Type and Scope Confusion - Beginner

Post by dodicat »

What and where is changelog.txt?
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Type and Scope Confusion - Beginner

Post by fxm »

With each FreeBASIC version, there is a "changelog.txt" file that summarizes all changes since the beginning of the development (in the same directory as "fbc.exe").
You can also find the daily version at https://github.com/freebasic/fbc or https://sourceforge.net/p/fbc/code/ci/master/tree/.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Type and Scope Confusion - Beginner

Post by dodicat »

So the compiler is being updated.
Do we have to recompile the compiler to make use of these fixes/updates?
If not, where can beginners get hold of the compiler?
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Re: Type and Scope Confusion - Beginner

Post by Lost Zergling »

Off Topic. Hi dodicat. New version of my list implementation in project. I am very interested in your technique around the use of macros. In particular, for the quick mapping of the elements of a list, I think that there may be a possible implementation for parallel processing instructions, that it is not a "macro hack" when one do not want to ask the processor for a resource-intensive synchronization task like with a thread. I started thinking about this, I think it may be feasible: MyList.HasMap (Key1, Key2, and so on) and then asynchronous parser on returned pointers :
whyle MyList.HashParse ...returning mapped pointers : Wend
ps : HashTag should be not far from "async ready"
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Type and Scope Confusion - Beginner

Post by fxm »

Official releases are here: News
Information on Git builds is here: Got builds
Post Reply