Type and Scope Confusion - Beginner
Type and Scope Confusion - Beginner
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
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
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: Type and Scope Confusion - Beginner
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
"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
Re: Type and Scope Confusion - Beginner
And indeed it doesn't, at least in the latest versions (1.05 and 1.06).D.J.Peters wrote:...
"WITH RequiredFiles" should not compile at all
RequiredFiles are a TYPE/STRUCT descriptor
but not a reference to any memory.
@Red2: what compiler version are you using? Perhaps yours is an old one...
Re: Type and Scope Confusion - Beginner
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.
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.
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: Type and Scope Confusion - Beginner
@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
I talk about a BASIC UDT not class with constructor's.
Joshy
Re: Type and Scope Confusion - Beginner
The only bug could be that the parser accepts "UDT" as a shortcut of "UDT()" ("UDT()" being the right syntax):
is fully legitimate.
A too smart parser, which interprets depending on context?
Code: Select all
With UDT()
.....
End With
A too smart parser, which interprets depending on context?
Re: Type and Scope Confusion - Beginner
Mmm... I don't know. What would be the point of that construct?fxm wrote:The only bug could be that the parser accepts "UDT" as a shortcut of "UDT()" ("UDT()" being the right syntax):is fully legitimate.Code: Select all
With UDT() ..... End With
Re: Type and Scope Confusion - Beginner
"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!
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!
Re: Type and Scope Confusion - Beginner
See changelog.txt:
Version 1.01.0
.....
{added]
.....
- WITH compounds now also accept type<UDT>(...) and UDT(...) expressions
.....
Re: Type and Scope Confusion - Beginner
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.
Re: Type and Scope Confusion - Beginner
What and where is changelog.txt?
Re: Type and Scope Confusion - Beginner
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/.
You can also find the daily version at https://github.com/freebasic/fbc or https://sourceforge.net/p/fbc/code/ci/master/tree/.
Re: Type and Scope Confusion - Beginner
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?
Do we have to recompile the compiler to make use of these fixes/updates?
If not, where can beginners get hold of the compiler?
-
- Posts: 538
- Joined: Dec 02, 2011 22:51
- Location: France
Re: Type and Scope Confusion - Beginner
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"
whyle MyList.HashParse ...returning mapped pointers : Wend
ps : HashTag should be not far from "async ready"