Support pointer (structure) as return value in procedures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Support pointer (structure) as return value in procedures

Post 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
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Support pointer (structure) as return value in procedure

Post 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
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Support pointer (structure) as return value in procedure

Post 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 ;-)
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Support pointer (structure) as return value in procedure

Post 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.
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Support pointer (structure) as return value in procedure

Post 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.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Support pointer (structure) as return value in procedure

Post 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.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Support pointer (structure) as return value in procedure

Post 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.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Support pointer (structure) as return value in procedure

Post 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. :lol:

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 :D


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
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Support pointer (structure) as return value in procedure

Post 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.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Support pointer (structure) as return value in procedure

Post 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
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Support pointer (structure) as return value in procedure

Post 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.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Support pointer (structure) as return value in procedure

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Support pointer (structure) as return value in procedure

Post 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.
quidquid Latine dictum sit altum videtur
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Support pointer (structure) as return value in procedure

Post 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
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Support pointer (structure) as return value in procedure

Post 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.
Post Reply