multiple assignment

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: multiple assignment

Post by skywalk »

Yes, I was downvoting multiple RETURN values.
Multiple assignment is barely OK, but I would never use it since I use automated parsing tools.
And I rarely use colons.
This is annoying, lazy and crazy: :evil:
Protected x$, y.d, z.i = "123", 123.00, 123
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
FlatEarth

Re: multiple assignment

Post by FlatEarth »

The Goto command is nonsense in my opinion, I never use it. But it exists in pb.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: multiple assignment

Post by Little John »

kenmo wrote: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
I agree that multiple assignment wouldn't reduce reliability.
But it can reduce readability, like it's shown in your example above.
The way we are writing the same stuff currently is considerably better readable:

Code: Select all

Define.d x=0.0, y=1.0, z=2.0
kenmo wrote: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.
Well, the ability of procedures to return multiple values would be the main advantage of all this.
The possibility of multiple assignment without the possibility of procedures returning multiple values is pretty pointless.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: multiple assignment

Post by kenmo »

Protected x$, y.d, z.i = "123", 123.00, 123
Yes, that is ugly. But you're choosing to make it that :)
It makes the most sense for pairs or triples of the same type, such as screen dimensions, xyz coordinates, etc.

It's very common for coordinates and vectors to be written as (a, b, c, ...) in mathematics, engineering, pseudocode, math-focused languages... it's not unreadable or confusing if it's just a few values of the same type.
Define.d x=0.0, y=1.0, z=2.0
That is pretty close to what is being requested here. Which is why (I like this feature request but) it's not urgent at all.
At least we don't have to write the word "Define" 3 times :)
Well, the ability of procedures to return multiple values would be the main advantage of all this.
The possibility of multiple assignment without the possibility of procedures returning multiple values is pretty pointless.
I don't think so. These are two separate features. One a minor syntax shorthand. The other a big change to how procedures work.
The language could implement one, the other, neither, or both.

Code: Select all

width, height = 640, 480 ; just simple shorthand, compiles the same as width=640 : height = 480
width, height = CalcSize() ; (these vars) = (unpack the multi-value result of a multi-return procedure)
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: multiple assignment

Post by Danilo »

kenmo wrote:It makes the most sense for pairs or triples of the same type, such as screen dimensions, xyz coordinates, etc.

It's very common for coordinates and vectors to be written as (a, b, c, ...) in mathematics, engineering, pseudocode, math-focused languages... it's not unreadable or confusing if it's just a few values of the same type.
I'm learning the Rust programming language recently (started 2 month ago),
and what you are talking about here is called Tuple's there:

Code: Select all

fn main() {
    let tup = (500, 6.4, 1);  // create tuple

    let (x, y, z) = tup;      // destructuring the tuple

    println!("The value of y is: {}", y);
}
Tuple's are represented using values/types between '(' and ')'.
It can be seen like an anonymous structure. Because there are no
struct member names, you can access the elements inside tup via tup.0, tup.1, tup.2, etc..
Of course Tuple's can be returned from functions as well, it is a real compound data type.

For some quick informations and to get an idea:
- Video: Rust Tutorial by Doug Milford => Rust Tuples
- Book: Rust By Example => Tuples
- Book: The Rust Programming Language => data types => The Tuple Type
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: multiple assignment

Post by kenmo »

Yep, I know tuples mostly from Python.
But like you said, it's a compound data type, which you can do all sorts of nice things with, but it's more complicated than the original feature request here (shorter initial assignments).
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: multiple assignment

Post by Little John »

kenmo wrote:
Well, the ability of procedures to return multiple values would be the main advantage of all this.
The possibility of multiple assignment without the possibility of procedures returning multiple values is pretty pointless.
I don't think so. These are two separate features. One a minor syntax shorthand. The other a big change to how procedures work.
The language could implement one, the other, neither, or both.
These two features, as proposed, are obviously related.

Code: Select all

Procedure MyProcedure()
   ProcedureReturn 3, 4
EndProcedure

a, b = MyProcedure()
In the code snippet above, a working multiple assignment is the prerequisite for MyProcedure() to return 2 values this way.

And without the possibility of procedures returning multiple values, just being able to write

Code: Select all

Define.d x, y, z = 0.0, 1.0, 2.0
instead of

Code: Select all

Define.d x=0.0, y=1.0, z=2.0
or something similar wouldn't be an advantage.

Danilo wrote:I'm learning the Rust programming language recently (started 2 month ago),
and what you are talking about here is called Tuple's there:

Code: Select all

fn main() {
    let tup = (500, 6.4, 1);  // create tuple

    let (x, y, z) = tup;      // destructuring the tuple

    println!("The value of y is: {}", y);
}
Yes, that is advantageous!
But that would mean introducing a new data type into PB, so it's also a bit more than just being able to write something like

Code: Select all

x, y = 100, 50
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: multiple assignment

Post by Little John »

I have to correct myself. Apart from the possibility that procedures could return multiple values, and apart from the introduction of new datatypes into PureBasic, the following would be a use of multiple assignment that would really be a benefit:

Code: Select all

Procedure test()
   Static a.Point = 1,2
   
   Debug a\x
   Debug a\y
EndProcedure
see this thread
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: multiple assignment

Post by Rinzwind »

Similar with array assignment. Some c code is very ugly to translate to PB because PB can't assign or pass arrays declared inline.

Don't expect any language features added to PB, cause it barely happened before AFAIK. Only library additions and a module command without a global scope specifier.
FlatEarth

Re: multiple assignment

Post by FlatEarth »

Little John wrote:I have to correct myself. Apart from the possibility that procedures could return multiple values, and apart from the introduction of new datatypes into PureBasic, the following would be a use of multiple assignment that would really be a benefit:

Code: Select all

Procedure test()
   Static a.Point = 1,2
   
   Debug a\x
   Debug a\y
EndProcedure
see this thread
+1
punak
User
User
Posts: 81
Joined: Tue Sep 07, 2021 12:08 pm

Re: multiple assignment

Post by punak »

+1
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: multiple assignment

Post by Rinzwind »

One can't even assign array values in one go, and you expect tuples? Way too modern for PB :twisted:
punak
User
User
Posts: 81
Joined: Tue Sep 07, 2021 12:08 pm

Re: multiple assignment

Post by punak »

I voted only for "multiple return" and "multiple assignment for variables".
I don't know the more complicated issues like arrays and so on , but I think the first two suggested are interesting.
This is my personal opinion.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: multiple assignment

Post by Demivec »

I know this is a little bit divergent from the original post but that seems to be the way things are going.

I think that the assignments involving structures have value. My proposed changes are not extensive to cover every possibility and fit more in line with syntactic sugar. I provide some examples but not necessarily useful examples. :P

Code: Select all

;proposed syntax

;a new 'list expression type' is a list of comma separated values or expressions between { and }.

; strings
Define m.i = 12
Define k.s = "start" + {-3, "middle", Str(3 + 5), m}
Debug k ; outputs start-3middle812

Debug {"hello", 5, 2.0, 5 - 3, "done"} ;outputs hello52.00000002done

;arrays, single dimension only
Dim g.i(7), n.Point(3)
g(1) = {1, 3, 5, 4, 2, 0} ; set array elements 1 through 6

n.point(0) = {{9, -2}, {3 + 6, 2 * g(3)}, {-2, height}} ; set array elements 0 through 2

;NOT addressed are  multidimensional array assignment. 
;The further syntactic complexities that would need to be addressed include
;row order or column order and the assignments of less than a full set of elements.


;lists
NewList h.Point()
AddElement(h())
h.Point() = {6, 3} ; assign list element

:structures
Structure foo
  a.s
  b.f
  c.i
  Array d.i(5) 
EndStructure

ReDim g(5) ; needs to match structure array size for next step
Define t.foo = {"real", 7.0, 2, g()} ; set structure elements

-1 for assignment of list of values to list of variables (more than one variable on left of equal sign)
+1 for multiple assignment to a structure or an array
Post Reply