Page 2 of 3

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

Posted: Fri Oct 29, 2010 3:54 pm
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.

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

Posted: Fri Oct 29, 2010 6:43 pm
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

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

Posted: Fri Oct 29, 2010 7:16 pm
by IceSoft
I longer discuss is not really useful.
Thanks for the fish. ;-)

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

Posted: Fri Oct 29, 2010 9:39 pm
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.

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

Posted: Fri Oct 29, 2010 10:49 pm
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 ;-)

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

Posted: Fri Oct 29, 2010 11:51 pm
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

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

Posted: Sat Oct 30, 2010 7:18 am
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.

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

Posted: Sat Oct 30, 2010 12:56 pm
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.

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

Posted: Mon Nov 01, 2010 12:18 am
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)

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

Posted: Fri Dec 17, 2010 8:41 am
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.

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

Posted: Fri Dec 17, 2010 9:00 am
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 :-)

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

Posted: Fri Dec 17, 2010 11:24 am
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.

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

Posted: Fri Dec 17, 2010 12:34 pm
by blueznl
One day I will understand the things you're saying, until then... euh... I'll just keep on hoping :-)

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

Posted: Wed Dec 22, 2010 2:35 pm
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.

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

Posted: Wed Aug 17, 2011 11:08 pm
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.