Page 1 of 1
Support a 'new' Syntax (Structure pointer as return value)
Posted: Fri Feb 08, 2013 9:49 am
by IceSoft
@Fred
Please implemet this new syntax for using of: structure pointer as return value:
(Currently PB returns a syntax error using this new 'keywords')
Code: Select all
Declare.*<Struct> foo()
DeclareC.*<Struct> foo()
DeclareCDLL.*<Struct> foo()
DeclareDLL.*<Struct> foo()
Procedure.*<Struct> foo()
ProcedureC.*<Struct> foo()
ProcedureCDLL.*<Struct> foo()
ProcedureDLL.*<Struct> foo()
ImportC
foo.*<Struct>() As "foo@16"
EndImport
Import
foo.*<Struct>() As "foo@16"
EndImport
Here an example how it can be used:
Code: Select all
Structure cpVect
x.d
y.d
EndStructure
Declare.*cpVect foo()
[do something]
Procedure.*cpVect foo()
*a.cpVect = AllocateMemory(SizeOf(cpVect))
*a\x = 11.1
*a\y = 22.1
ProcedureReturn *a
EndProcedure
Re: Support a 'new' Syntax (Structure pointer as return valu
Posted: Fri Feb 08, 2013 11:39 am
by Danilo
I use this currently:
Code: Select all
Structure Vect
x.d
y.d
EndStructure
Macro pVect : i : EndMacro
Declare.pVect foo()
[do something]
Procedure.pVect foo()
*a.Vect = AllocateMemory(SizeOf(Vect))
*a\x = 11.1
*a\y = 22.1
ProcedureReturn *a
EndProcedure
I like to add something to the wishlist here, because it is similar:
Please allow use of Interfaces in Declare, Prototype and Procedure return type declarations.
Code: Select all
Interface IDirectX14
create()
EndInterface
Procedure.IDirectX14 Blah()
; blub
EndProcedure
It is just a pointer, so it shouldn't be a problem for Interfaces.
Currently we get the following error message: "A structure can't be used with ProcedureReturn.", which does not make sense at all.
There is no structure used in the code, and there is no ProcedureReturn!
So, allowing Interfaces as return type should be easy, as it's a pointer only anyway...?
For returning real Pointers, IceSoft's example doesn't look so bad to me. Common PureBasic Syntax, everybody will understand it.
Re: Support a 'new' Syntax (Structure pointer as return valu
Posted: Fri Feb 08, 2013 2:37 pm
by IceSoft
Danilo wrote:For returning real Pointers, IceSoft's example doesn't look so bad to me. Common PureBasic Syntax, everybody will understand it.
Thanks!
And it can be very easy internal replaced with ".i" before compiler and syntax check is running over the source file.
Here the small regular expression used internal which improve the PureBasic syntax:
Code: Select all
If CreateRegularExpression(0, "\.\*.*?[ (]")
a.s = ReplaceRegularExpression(0, source, ".i")
MessageRequester("Info", "Structure pointer syntax replaced here: "+a)
EndIf
Re: Support a 'new' Syntax (Structure pointer as return valu
Posted: Fri Feb 08, 2013 7:02 pm
by freak
Re: Support a 'new' Syntax (Structure pointer as return valu
Posted: Fri Feb 08, 2013 7:54 pm
by IceSoft
An answer like this is also annoying. So stop it please too. Thanks.
Re: Support a 'new' Syntax (Structure pointer as return valu
Posted: Fri Feb 08, 2013 7:59 pm
by Fred
It is a common rule to not open several times the same topic on a forum, or it will get messy very quickly.
Re: Support a 'new' Syntax (Structure pointer as return valu
Posted: Fri Feb 08, 2013 8:42 pm
by freak
IceSoft wrote:
An answer like this is also annoying. So stop it please too. Thanks.
Seriously, do you think your chances of getting things implemented are better when you annoy the hell out of the people you are asking? I don't think so.
Re: Support a 'new' Syntax (Structure pointer as return valu
Posted: Fri Feb 08, 2013 9:39 pm
by IceSoft
freak wrote:IceSoft wrote:
An answer like this is also annoying. So stop it please too. Thanks.
Seriously, do you think your chances of getting things implemented are better when you annoy the hell out of the people you are asking? I don't think so.
I am not sure. But anyhow I think you know about this whish and how easy the implementation is

Ok I know YOU are the master and I am only one of your smallest slave of course

Re: Support a 'new' Syntax (Structure pointer as return valu
Posted: Sat Feb 09, 2013 8:57 pm
by RichAlgeni
This reply isn't to tell people what to do, and it's not criticize anyone's coding practices. It's just that my experience may be helpful to some.
All my procedures return an integer, positive for successful, 0 for unsuccessful, negative for an error.
Any data that is needed in both the parent and daughter procedure is instantiated (created, if you will), in the parent process. Strings are allocated, number are initialized, etc.
Never pass strings to a procedure, always pass pointers. If a structure needs to be processed in some way, initialize the structure in the calling procedure, pass the pointer, then check to see if the returned variable is a positive integer.
If an unknown amount of data will be sent back to a parent procedure, set a variable with the maximum length in the parent, allocate memory to this length, plus one byte for a terminator, then pass this length and the pointer to the procedure.
Always, always use type '.i' for integers, even if you need just one digit, it will save you in the long run.
Like I said, this is just the (maybe) benefit of my experience, but by using this practices, I have just about eliminated all IMA errors in my code, my procedures are standardized, (see functional coding), and I never have to worry about returning structures or pointers back to a calling process.