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 »

@Fred,
Any hope to support it with the next PureBasic release?
(I belive it is a small change for PureBasic and the community will get a big new feature)
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

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

Post by Shield »

I'd actually support this. :)

Recently I was fooling around with interfaces minding my own business only to find out that,
unfortunately, returning a structured pointer is not possible.

Because PB is only supporting void* pointers, things like "call chains" are not supported
which would be a very great feature.

Say you have an interface representing some kind of XML document which contains a procedure GetChildrenByID().
GetChildrenByID() would return a reference to the sub element referenced by the specified ID.

If PB supported this, you could do things like this directly:

Code: Select all

; Given this XML document...
; <?xml version="1.0" encoding="utf-8" ?>
; <root>
;     <person id="Mike">
;         <spec id="Info">
;             <age>21</age>
;         </spec>
;     </person>
; </root>

; ...and given everything has been initialized properly...
Debug *document\GetChildrenByID("Mike")\GetChildrenByID("Info")\GetValue("age") ; Returns '21'.

I hope my example shows the point why something like this isn't just "cosmetics".
Currently, we need to write the returning value of every procedure in a temporary variable
to move on to the next step of the 'calling process'. Like here:

Code: Select all

Define *element.IDocumentElement
*element = *document\GetChildrenByID("Mike")
*element = *element\GetChildrenByID("Info")
Debug *element\GetValue("age")

Assigning the content of a structured variable to another is already supported
but returning structured values still isn't.

So I'd very much like to see this feature implemented as it eases up the programming
(and isn't just yet another library function but a compiler enhancement ;) ).


I don't know about PureBasic's compiler internals but I guess it could take quite a few changes
to actually implement this, so adding a cast operator might be easier after all.

Code: Select all

Cast(IDocumentElement, Cast(IDocumentElement, *document\GetChildrenByID("Mike"))\GetChildrenByID("Info"))\GetValue("age")

Doesn't look half as pretty but it does the job. :)
Another downside is that this isn't type safe as you can cast any pointer value to any other type.
But then again we don't have true / full type safety in PB anyway. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
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 seems my whish will be come true...in the next PB version
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Post Reply