Using/With/Try&Finally/... or similar
Posted: Mon May 01, 2017 6:59 am
Hello,
As we currently discuss the Try/Catch, there is a very connected keyword in other languages that I would suggest for PureBasic. In C# it is Using, in python it is With. If we cannot have Try/Catch, we may still have a kind of centralized clean up inside of scopes.
It looks like this:it is equal toSo when leaving the scope, the buffer will be released automatically. In situations similar to the following two two it is useful, otherwise you'd have to care about cleaning up resources inside the If-blocks in this scope every single time:
However, the FreeMemory is somewhat too static, so a kind of destructor should be called instead. As PureBasic is not object oriented so much, a destructor call for each object would be kind of inconsistent and you may need to access variables of the outer scope anyway during cleanup. So I would prefer the Finally construct instead, even if Catch may not be implemented (in the near future).
As we currently discuss the Try/Catch, there is a very connected keyword in other languages that I would suggest for PureBasic. In C# it is Using, in python it is With. If we cannot have Try/Catch, we may still have a kind of centralized clean up inside of scopes.
It looks like this:
Code: Select all
Using *Buffer[ = AllocateMemory(...)]
; use buffer
EndUsing
Code: Select all
[*Buffer = AllocateMemory(...)]
Try
; use buffer
Finally
; Clean up the object
FreeMemory(*Buffer)
EndTry
Code: Select all
; Situation 1
Procedure Whatever()
Using *Buffer
...
If something
...
ProcedureReturn ...
EndIf
...
If something
...
ProcedureReturn ...
EndIf
...
EndUsing
EndProcedure
; Situation 2
For i = 0 To 20
Using *Buffer
...
If something
...
Break
EndIf
...
If something else
...
Continue
EndIf
...
EndUsing
Next