Page 1 of 3
Support pointer (structure) as return value in procedures
Posted: Thu Oct 28, 2010 6:46 am
by IceSoft
@Fred,
Please support Structure-Pointer as return value (see example it work but I have to declare the procedure return value as Integer)
Maybe adapting the syntaxcheck and find a nice syntax (e.g.: Procedure.aStr* or Procedure.*aStr) and it more easier to write wrappers for PureBasic.
Thanks.
Code: Select all
Structure aStr
b.l
a.i
EndStructure
; Procedure.aStr* test() <<<<<<<<<<<<<<<<<< Compiler syntax error
; ddd.aStr
; ddd\a = 100
; ddd\b = 10
; ProcedureReturn @ddd
; EndProcedure
Procedure.i test()
Static ddd.aStr
ddd\a = 100
ddd\b = 10
ProcedureReturn @ddd
EndProcedure
*bb.aStr = test()
Debug *bb\b
Debug *bb\a
Re: Support pointer (structure) as return value in procedure
Posted: Thu Oct 28, 2010 6:53 am
by helpy
It will also work (and it is allowed!!!) if you write:
Code: Select all
Procedure test()
....
EndProcedure
You do not have to give .i as return type!
.i is the default return type if you write nothing!
cu,
guido
Re: Support pointer (structure) as return value in procedure
Posted: Thu Oct 28, 2010 10:56 am
by IceSoft
helpy wrote:It will also work (and it is allowed!!!) if you write:
Code: Select all
Procedure test()
....
EndProcedure
You do not have to give .i as return type!
.i is the default return type if you write nothing!
cu,
guido
ยด
If you think this is ok?
If yes than we should have only one type for all

Re: Support pointer (structure) as return value in procedure
Posted: Thu Oct 28, 2010 12:24 pm
by Thorium
IceSoft wrote:
If you think this is ok?
If yes than we should have only one type for all

You cant have one type for all.
All that is the size of Integer or less will be return in EAX register. So there is no difference in returing .i or .b. But if the datatype is bigger like quad on x86 the compiler need to take special care of that.
Re: Support pointer (structure) as return value in procedure
Posted: Thu Oct 28, 2010 1:07 pm
by IceSoft
Thorium,
You should read about the: var (C#-Referenz)
The .i is not wrong but bad.
If I want to create automatic online help file from a wrapper for PureBasic I have to adapt the return parameter info. That is not really usefull.
Re: Support pointer (structure) as return value in procedure
Posted: Thu Oct 28, 2010 9:34 pm
by Mistrel
Thorium wrote:IceSoft wrote:So there is no difference in returing .i or .b.
How can you fit a 32/64-bit pointer into an 8-bit byte?
It will just wrap around.
Re: Support pointer (structure) as return value in procedure
Posted: Thu Oct 28, 2010 9:48 pm
by Thorium
Mistrel wrote:Thorium wrote:So there is no difference in returing .i or .b.
How can you fit a 32/64-bit pointer into an 8-bit byte?
It will just wrap around.
Forget what i wrote, it's wrong. ^^
I meant there is no difference in speed and memory usage. As it will be returned in EAX/RAX, no matter if it's .i or .b.
Re: Support pointer (structure) as return value in procedure
Posted: Fri Oct 29, 2010 12:10 am
by PMV
IceSoft wrote:Please support Structure-Pointer as return value (see example it work but I have to declare the procedure return value as Integer)
Maybe adapting the syntaxcheck and find a nice syntax (e.g.: Procedure.aStr* or Procedure.*aStr) and it more easier to write wrappers for PureBasic.
You should try a OOP-Language.
PB does only return a 32-Bit (or 64-Bit) value. PB does not know
what structure is behind the pointer. If you want such functionality,
there are a lot of OOP-Languages available.
You could wish a Pointer-Type .p to define, that you want to return
a pointer, not only a value. But for me, it would also be to OOP-like
btw.:
PB-Help wrote:A procedure can return a value or a string if necessary. You have to set the type after Procedure and use the ProcedureReturn keyword at any moment inside the procedure. A call of ProcedureReturn exits immediately the procedure, even when its called inside a loop.
MFG PMV
Re: Support pointer (structure) as return value in procedure
Posted: Fri Oct 29, 2010 5:33 am
by IceSoft
I dont want realy a functionality.
I want only a more friendly PureBasic compiler:
1. New Keyword for pointers (not i) => better: Procedure.*<type>
2. Compiler substituite: Procedure.*<type> with this one: Procedure.i intern
3. No compiler warning it I use Procedure.*<type>
Thats all and it improve the reading of PureBasic source code too.
Re: Support pointer (structure) as return value in procedure
Posted: Fri Oct 29, 2010 10:25 am
by PMV
So, you only want to write this, because your code looks better?
What do you think about this?
Code: Select all
Macro p
i
EndMacro
Structure aStr
b.l
a.i
EndStructure
Procedure.p test()
ddd.aStr
ddd\a = 100
ddd\b = 10
ProcedureReturn @ddd
EndProcedure
*bb.aStr = test()
Debug *bb\b
Debug *bb\a
The star isn't allowed, but you could wish to be allowed in
macros or structurenames
MFG PMV
Re: Support pointer (structure) as return value in procedure
Posted: Fri Oct 29, 2010 11:14 am
by IceSoft
PMV wrote:SoThe star isn't allowed, but you could wish to be allowed in
macros or structurenames
Only with <= PB 4.51.
It is better we need no macro on this place.
Re: Support pointer (structure) as return value in procedure
Posted: Fri Oct 29, 2010 12:23 pm
by Thorium
Well the point of a pointer is that it's not fixed to a structure. You can let it point to what ever structure you want. So defining the return value as a structure would be wrong. And i dont think it looks much better. If you want to keep track about what structures the pointer can point to, just place a comment in front of the procedure. Thats what i do.
Re: Support pointer (structure) as return value in procedure
Posted: Fri Oct 29, 2010 12:30 pm
by freak
We're not adding compiler features just for cosmetics. If you want your code to look different, just use macros. That's what they are for.
Re: Support pointer (structure) as return value in procedure
Posted: Fri Oct 29, 2010 1:27 pm
by IceSoft
Is is not for cosmetic. Never!
It is a feature which improve the compiler...if you make a type check:
Code: Select all
ProcedureReturn.i foo1()
tt.aStruc1
....
ProcedureReturn @tt.aStruc1
EndProcedure
ProcedureReturn.aStruc1 foo2()
tt.aStruc1
....
ProcedureReturn @tt.aStruc1
EndProcedure
*b.Struc1 = foo1()
*c.Struc2 = foo2() <<< Compiler can make a warning (*variable.Struc2 <> *RetrunValue.Struc1)
*i is more a void pointer
Re: Support pointer (structure) as return value in procedure
Posted: Fri Oct 29, 2010 2:16 pm
by Thorium
Why should it output a warning? It's legal to return the pointer, even if it poits to another structure.
Btw. your code does not work because you are using fixed structures. They do not exist any longer after the procedure is terminated. You need to use pointers and allocate memory for the structures.