Pass by reference

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
USCode
Addict
Addict
Posts: 912
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle, USA

Re: Pass by reference

Post by USCode »

Demivec wrote: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.
Looking at Mistral's first 2 examples in his original post, utilizing the pass by reference character "&" seems much easier IMHO than using @ and pointers in the second example.

Clearly this:

Code: Select all

Procedure That(&This)
  This=2
EndProcedure

That(This)
Debug This
Is simpler than this:

Code: Select all

Procedure That(*This.Integer)
  *This\i=2
EndProcedure

That(@This)
Debug This
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

USCode wrote:Clearly this:
...
Is simpler than this:
...
Kaeru Gaman wrote:working with structured pointers make it ALWAYS clearly visible that you are handling a byRef Value and no local Variable....
in your second example, wich is only nanometers shorter, it not self evident in Line 2 wich kind of Variable it is.
there is no way to tell by this line only that it is not access byVal.

... in the example that shows how it is now, you can clearly see that it is a Reference.
oh... and have a nice day.
User avatar
Psychophanta
Addict
Addict
Posts: 4996
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Pass by reference

Post by Psychophanta »

Demivec is very right here, as usual.
USCode wrote: Clearly this:

Code: Select all

Procedure That(&This)
  This=2
EndProcedure

That(This)
Debug This
Is simpler than this:

Code: Select all

Procedure That(*This.Integer)
  *This\i=2
EndProcedure

That(@This)
Debug This
Very obscure clairvoyance, indeed. :shock:
Kaeru and Demivec explains quite well, don't need for me to say samethings.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
USCode
Addict
Addict
Posts: 912
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle, USA

Post by USCode »

In my personal past experiences coding with Delphi and VB I've found a by-reference operator to be very useful and not confusing at all but I guess it's all academic as it's ultimately up to Fred and as he hasn't added that feature to the language yet expresses his opinion on the subject... 8)
User avatar
Lewis
User
User
Posts: 47
Joined: Fri Nov 25, 2005 1:12 pm

Post by Lewis »

I believe this conversation is quite similar to the one in hand:

HTH.
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Pass by reference

Post by buddymatkona »

The option to pass variables ByRef would make PureBasic much friendlier and probably increase the user base. I know people would hate it if
X = Pow(X, 3.0)
would not work unless X was part of a structure. I feel the same way about Procedure ModifyABCD(A,B,C,D).
Naampie2000
New User
New User
Posts: 3
Joined: Wed Jun 03, 2020 3:51 pm

Re: Pass by reference

Post by Naampie2000 »

I came across this topic searching this,

Wouldn't it be much simpler to pass by reference by default and yust move the value into a named varaible like so?

Code: Select all


DeclareModule featurerequest
Declare Ar
EndDeclareModule

Module featurerequest (a,b,c) ;<-byreference
Ar = a ;<- byvalue
EndModule

Naampie2000
New User
New User
Posts: 3
Joined: Wed Jun 03, 2020 3:51 pm

Re: Pass by reference

Post by Naampie2000 »

Hi,

I want to strengthen my case a little bit, my code always uses the ByRef keyword on 99.999% of the code.
And most of my modules are fire and forget. Even in cases where it matters to have a discrete variable, it's hardly inside the module body
for where it should require a discrete instance of the module.

Code: Select all

Module counter (ByRef register, ByRef increment)
register = register + increment
EndModule

Define CountA
counter (CountA, somevar)
;CountA = updated

Code: Select all

DeclareModule counter
Define register
EndDeclareModule

Module counter (byref increment)
register = register + increment
EndModule

counterN  (somevar) ; <- needs a unique instance of counter
Being used to this, it would be hard to look back,
And would be like having a dinner at a fancy restaurant getting every meal prewrapped to go, for the rarest case people just up and leave the well suited establishment.. in order to "return".

I would really like a feature like this, maybe in order to have it backwards compatible in the form of a derivative?




Kind regards,
Last edited by Naampie2000 on Thu Jun 04, 2020 2:55 pm, edited 4 times in total.
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Pass by reference

Post by NicTheQuick »

This is not Purebasic code you are talking about.
Also I don't understand what your point should be. :?:
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Naampie2000
New User
New User
Posts: 3
Joined: Wed Jun 03, 2020 3:51 pm

Re: Pass by reference

Post by Naampie2000 »

The code is written as a feature request,
Why things could be passed by reference "by default" in my oppinion, is my code simply looks like that.
Else there's the way of using the module header for discrete variable declaration.

In order to do the module as it stands, every pointer passed to it is converted to a move instruction into a discrete register.
Which you can resolve by proceding to undo it by butchering your syntax by putting * and \ everywhere.
Which has the drawback of producing semi legible "slow by default" code and some extra work to make it.

The other way around, "with the header passing pointers by default" or having byref syntax like &.
None of these syntax issues exist. And the rare case of a move instruction being needed, you can declare and move a variable.

The pro's of doing this are writing fast code fast and legible.
The only con I can think of is backwards compatability, but even that may not be so much of an issue.
So if it was a left side opperand it was bound to change anyway.
Post Reply