Page 1 of 2
multiple assignment
Posted: Mon Apr 27, 2020 5:27 am
by FlatEarth
Hi
The requested feature is specified in the codes.
.
.
.
Re: multiple assignment
Posted: Mon Apr 27, 2020 8:18 am
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
Re: multiple assignment
Posted: Mon Apr 27, 2020 8:39 am
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)
Re: multiple assignment
Posted: Mon Apr 27, 2020 4:17 pm
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.
Re: multiple assignment
Posted: Mon Apr 27, 2020 4:28 pm
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.
Re: multiple assignment
Posted: Tue Apr 28, 2020 5:30 am
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.
Re: multiple assignment
Posted: Wed Apr 29, 2020 2:40 am
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
Re: multiple assignment
Posted: Wed Apr 29, 2020 3:42 am
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".
Re: multiple assignment
Posted: Wed Apr 29, 2020 3:17 pm
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.
Re: multiple assignment
Posted: Sun May 03, 2020 11:51 am
by Caronte3D
+1
Re: multiple assignment
Posted: Sun May 03, 2020 12:51 pm
by IdeasVacuum
-1
It's an unnecessary complexity compiler side? Too many unnecessary features and what do you get? Reduced reliability.
Re: multiple assignment
Posted: Sun May 03, 2020 1:13 pm
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.
Re: multiple assignment
Posted: Sun May 03, 2020 1:37 pm
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
Re: multiple assignment
Posted: Sun May 03, 2020 2:01 pm
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
Re: multiple assignment
Posted: Sun May 03, 2020 2:55 pm
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:
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.