Mistrel wrote:Am I missing some simple solution here or is this type of parameter passing too complex for PureBasic to handle gracefully?
Mistrel wrote:It's a great convenience, I think. In my opinion it can make code that involves a lot of dynamic allocation and pointer-passing much easier to read and write.
In looking at the examples you provided nothing you did made the code any easier to read. The ways that you suggested to make it easier to write seemed to only involve exchanging the "@" character for an "&" character. That hardly seems helpful or worthwhile.
Side note: IMHO your best bet to making it easier to read is to use more meaningful variables and procedure names.
Here's my version of the same example procedure:
Code: Select all
Structure Something
a.i
b.i
c.i
EndStructure
Procedure newSomething(*ptrThis.Integer)
Protected *this.Something = AllocateMemory(SizeOf(Something))
*ptrThis\i = *this
*this\a = 1
*this\b = 2
*this\c = 3
EndProcedure
Define *this.Something
newSomething(@*this)
Debug *this\a
Debug *this\b
Debug *this\c
To show how this compares to what you suggested, here they are side by side:
Code: Select all
Structure Something
a.i
b.i
c.i
EndStructure
Procedure newSomething(*ptrThis.Integer) Procedure That(&*This.Something)
Protected *this.Something = AllocateMemory(SizeOf(Something)) *This=AllocateMemory(SizeOf(Something))
*ptrThis\i = *this
*this\a = 1 *This\a=1
*this\b = 2 *This\b=2
*this\c = 3 *This\c=3
EndProcedure EndProcedure
Define *this.Something Define *This.Something
newSomething(@*this) That(*This)
Debug *this\a Debug *This\a
Debug *this\b Debug *This\b
Debug *this\c Debug *This\c
I count only one line of difference. You be the judge. I favor this method over the one tinman suggested because it doesn't require creating additional structures.
USCode wrote:Many folks don't want to deal directly with pointers and a simple pass by reference keyword would be much appreciated
Since performing these operations means you
are working with pointers it wouldn't matter if you used "
NotAPtr this.Something" instead of "
*this.Something", because you have to remember where the values are being read and written to. As noted previously using an "&" in the definition of a procedure parameter is just as hard as using "@" in a procedure call. The person using it needs to know why they are using it.
- 1
It seems a suggestion for redundant functionality that is already well covered by "*" pointers and the "@" operator and wouldn't help anyone who is already confused about using pointers.
@Edit: added the word "help" to the last sentence, it had been omitted as an oversight.