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

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

Post by IceSoft »

It is a check during compile time...not runtime.
this check have only to do if there a structurepointer expected.
The i is a void pointer and need no check.

Open your mind and you understand it.
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 »

IceSoft wrote:I dont want realy a functionality.
[...]
Thats all and it improve the reading of PureBasic source code too.
IceSoft wrote:Is is not for cosmetic. Never!
It is a feature which improve the compiler...if you make a type check:
:?

so i will repeat it:
PMV wrote: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
:wink:

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 longer discuss is not really useful.
Thanks for the fish. ;-)
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:It is a check during compile time...not runtime.
this check have only to do if there a structurepointer expected.
The i is a void pointer and need no check.

Open your mind and you understand it.
I understand it. I just dont agree with you.

Actualy some of my sources would generate a warning with your check, even if there is nothing wrong.

Here is an example:

Code: Select all

Structure Pixel
  Blue.a
  Green.a
  Red.a
  Alpha.a
EndStructure

Procedure.i GetPixel()
  
  Protected *Pixel.Pixel
  
  *Pixel = AllocateMemory(SizeOf(Pixel))
  
  *Pixel\Blue  =  50
  *Pixel\Green =  60
  *Pixel\Red   =  50
  *Pixel\Alpha = 255
  
  ProcedureReturn *Pixel
  
EndProcedure

Define *Pixel.Long

*Pixel = GetPixel()
That would generate a warning, just because i access the pixel inside the procedure with a structure that splits the channels and outside of the procedure as a long.

I get why you want that, but i disagree on the importance of that feature. It's only usefull to detect if you used a wrong structure name. Actualy that never happend to me, not one time.
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 wrote:

Code: Select all

Structure Pixel
  Blue.a
  Green.a
  Red.a
  Alpha.a
EndStructure

Procedure.i GetPixel()
  
  Protected *Pixel.Pixel
  
  *Pixel = AllocateMemory(SizeOf(Pixel))
  
  *Pixel\Blue  =  50
  *Pixel\Green =  60
  *Pixel\Red   =  50
  *Pixel\Alpha = 255
  
  ProcedureReturn *Pixel
  
EndProcedure

Define *Pixel.Long

*Pixel = GetPixel()
Maybe the check is part of option explicit. So no warning will raising with your code.
But I belive your code is realy a good example why a check is usefull ;-)
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 »

You doesn't understand the code? :?
Your wish would brake this fully functional code.
You know, that a 32-Bit color is only this number of 4 unsigned Bytes.
You can pass this value to every command, that needs a color.

Code: Select all

Structure Pixel
  Blue.a
  Green.a
  Red.a
  Alpha.a
EndStructure
Procedure.i GetPixel()
  
  Protected *Pixel.Pixel
  
  *Pixel = AllocateMemory(SizeOf(Pixel))
  
  *Pixel\Blue  =  $F0
  *Pixel\Green =  $3A
  *Pixel\Red   =  $40
  *Pixel\Alpha =  $FF
  
  ProcedureReturn *Pixel
  
EndProcedure

Define *Pixel.Long

*Pixel = GetPixel()

Debug Bin(*Pixel\l, #PB_Long)
Debug Bin($FF403AF0, #PB_Long)
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 understand your code. It is a bad one.

Better:

Code: Select all

Define *Pixel.Pixel
And you get intelisense from the editor too ;-)
Please try it.
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:I understand your code. It is a bad one.

Better:

Code: Select all

Define *Pixel.Pixel
And you get intelisense from the editor too ;-)
Please try it.
Well the point is to not use the Pixel structure, because it can be easier to handle a pixel as a long in some situations and in some its easier to handle it as a structure of channels. Why is that bad? It's totaly legal and can improve readability.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post by blueznl »

If...

Code: Select all

a.struct = b.struct
... is allowed, then it would make somehow sense to be able to do something like...

Code: Select all

Procedure.struct test(x)
  ...
  ProcedureReturn something.struct
EndProcedure

a.struct = test(x)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
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 »

blueznl wrote:If...

Code: Select all

a.struct = b.struct
... is allowed, then it would make somehow sense to be able to do something like...

Code: Select all

Procedure.struct test(x)
  ...
  ProcedureReturn something.struct
EndProcedure
a.struct = test(x)
Maybe you are right for 'byValues' returns.

But not for pointers. Look this is working:

Code: Select all

*a.aStr = *b.aStr
Why not also for functions too. It is only the syntax checker which let not use it.
Syntax error is on the definition of the procedure here:

Code: Select all

declare.*aStr test(x)
*a.aStr = test(x)
And no! It is not only a cosmetic improvement! It is much more!

Yes I know I can make it like this:

Code: Select all

declare.i test(x)
but this is more a void* pointer like in C.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post by blueznl »

Yeah, it would not work well for a pointer, I know. But for a structure it should work well, don't you think so?

As for a pointer, a pointer is just that: a pointer.

So, if you return a pointer, you return a numeric value, which actually points to something in memory. The returned value does not have a type (beyound being a pointer, that is).

What's stored at the memory location the pointer points to can't be told in the procedure declaration or its return value.

Hmmm.

Which means, that returning a pointer works fine as it is :-) I only want to add returning a structure, and I guess that should be within the logical limitations of the language... let pointers live and be free, I'd say :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
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 »

What I mean is: PureBasic supports only void* pointers on declaration.
Later you can use (cast) each void* (=.i) pointer.

My wish is: Support also "pointer to a structure" during declaration.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post by blueznl »

One day I will understand the things you're saying, until then... euh... I'll just keep on hoping :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
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 »

blueznl wrote:One day I will understand the things you're saying, until then... euh... I'll just keep on hoping :-)
Ok here an actual example of my Chipmunk wrapper:

Code: Select all

  cpArbiterAlloc.i() As ...
  cpArbiterInit.i(*par1.cpArbiter, *par2.cpShape, *par3.cpShape) As ...
  cpArbiterNew.i(*par1.cpShape, *par2.cpShape) As ...
You see: the return valiu is always ".i" which mean (integer or void* pointer)

If PureBasic supports also return values of "pointer to a structure" I can make the reading of the wrapper better:

Code: Select all

  cpArbiterAlloc.cpArbiter() As ...
  cpArbiterInit.cpArbiter(*par1.cpArbiter, *par2.cpShape, *par3.cpShape) As ...
  cpArbiterNew.cpArbiter(*par1.cpShape, *par2.cpShape) As ...
Ok the sytax can be looks like .*cpArbiter or as you see on the example above.

Hope you understand something about what I mean better now.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
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 »

@Fred/@freak,
Any change to add this feature to the PureBasic?
At least It will improve all wrapper and of course something more.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Post Reply