multiple assignment

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
FlatEarth

multiple assignment

Post by FlatEarth »

Hi
The requested feature is specified in the codes.

Code: Select all

Define a,b,c = 1,2,3

Code: Select all

Global  a,b,c = 1,2,3

Code: Select all

Protected a,b,c = 1,2,3
.
.
.
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: multiple assignment

Post by BarryG »

Why? What's wrong with the following? Your version makes it hard to see which value goes with which variable (especially if you have lots of variables on each line).

Code: Select all

Define a=1,b=2,c=3
Global a=1,b=2,c=3
Protected a=1,b=2,c=3
FlatEarth

Re: multiple assignment

Post by FlatEarth »

Not always, but sometimes this syntax is easier. Its existence is harmless and useful. this feature is also available in the lua language.

It may not be very relevant but, the multi-return feature is also useful.

Code: Select all

Procedure MultiRet(a,b,c)
   ProcedureReturn a,b,c
EndProcedure
 
Global a,b,c = MultiRet(10,20,30)
glennj.0158
User
User
Posts: 21
Joined: Tue Mar 31, 2020 4:43 pm
Location: Lawrenceville, NJ, USA

Re: multiple assignment

Post by glennj.0158 »

I agree that the construction obfuscates the result. To determine which variable got which value you need to count in. The existing syntax means I just need to find the variable and it's value is immediately apparent.

On the topic of multiple returns, I understand the need to return multiple values. In other languages the CALL command often supports call-by-name in addition to call-by-value.

I have not done it yet but the @ operator purports to get the address of a structure, array, variable or constant. Using that you can pass the address of the variable(s) to be altered in the procedure and "return" as many variables as you want using a clearly defined method.
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: multiple assignment

Post by skywalk »

Multiple returns are problematic with equations and a procedure in the expression.
y = cos(2*MyMultiReturnFn(x,y,z)) + sin(MyMultiReturnFn(x,y,z)/2)
What is your time saving with this feature?
Remember, you can return a structure or modify the called parameters using @x,@y,@z.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: multiple assignment

Post by Mistrel »

You can't return structures as a copy-assignment; only pointers too structures either as a return value or by passing an address as an argument.
glennj.0158
User
User
Posts: 21
Joined: Tue Mar 31, 2020 4:43 pm
Location: Lawrenceville, NJ, USA

Re: multiple assignment

Post by glennj.0158 »

skywalk wrote:Remember, you can return a structure or modify the called parameters using @x,@y,@z.
I tried passing an address as an argument and it didn't work as I expected. I posted the question and the overall answer was that PureBasic does not support indirect reference and you would need to use the Poke(Pointer, ValueToStore) function to actually store information into an external simple variable.

If you have a counter example please share.

Regards
Glenn
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: multiple assignment

Post by skywalk »

Yes, I read your reply in that topic.
I showed you how to pass ByRef in the Procedure. There are many fine examples in the forum if you search "ByRef".
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Blue
Addict
Addict
Posts: 886
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: multiple assignment

Post by Blue »

+1
Excellent suggestion, FlatEarth.
His suggestion does not imply that there's something wrong with the existing coding rules or habits.
It would simply add a quick way of doing the same thing.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Caronte3D
Addict
Addict
Posts: 1055
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: multiple assignment

Post by Caronte3D »

+1
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: multiple assignment

Post by IdeasVacuum »

-1
It's an unnecessary complexity compiler side? Too many unnecessary features and what do you get? Reduced reliability.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 281
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

Re: multiple assignment

Post by oreopa »

-1. Can't really see a need for this.
glennj.0158 wrote:To determine which variable got which value you need to count in.
^ This.
Proud supporter of PB! * Musician * C64/6502 Freak
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: multiple assignment

Post by StarBootics »

oreopa wrote:-1. Can't really see a need for this.
Me too when I have a need for Multiple return I prefer to do something like this :

Code: Select all

Procedure MultipleReturn(a.f, b.f, c.f, *Return00_Float, *Return01_Float)
  
  PokeF(*Return00_Float, ACos(a))
  PokeF(*Return01_Float, ATan2(b, c))  
  
EndProcedure

MultipleReturn(0.25, 0.25, 0.35, @Return00.f, @Return01.f)

Debug Return00
Debug Return01
The Stone Age did not end due to a shortage of stones !
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: multiple assignment

Post by mk-soft »

Maybe forgotten, but you do not need PokeF.

You can specify the VarType for ByRef, like a structure.

Code: Select all

Procedure MultipleReturn(a.f, b.f, c.f, *Return00_Float.float, *Return01_Float.float)
  
  *Return00_Float\f = ACos(a)
  *Return01_Float\f = ATan2(b, c)  
  
EndProcedure

MultipleReturn(0.25, 0.25, 0.35, @Return00.f, @Return01.f)

Debug Return00
Debug Return01
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: multiple assignment

Post by kenmo »

+1 for multiple assignment

1. What's wrong with the existing syntax?
Nothing. It's just objectively longer. The team has added many syntax shorthands over the years.

2. It is too long / hard to read / follow?
It is used all the time in other languages with no problem :!:
especially for pairs or triples, such as

Code: Select all

width, height = 640, 480
x, y, z = 5, 10, 20
If you choose to make your line 10+ definitions and difficult to read, that's your own fault.
You can already do that with colons and putting 10+ commands or data items on one line.

3. It would reduce reliability?
It would have zero change on the compiled executable.
The team has implemented features 100x more complex than this.
I think they can handle parsing this:

Code: Select all

Define.d x, y, z = 0.0, 1.0, 2.0
into this:

Code: Select all

Define.d x = 0.0
Define.d y = 1.0
Define.d z = 2.0
if they choose. It's simpler than macro expansion.



PS. I think we complicated this thread by bringing up multiple returns from procedures.

THAT is a much bigger change, a fundamental change to the language. It would affect the compiler, the syntax, the debugger, and require much more testing.
Post Reply