Assignment expressions

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
jacdelad
Addict
Addict
Posts: 1492
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Assignment expressions

Post by jacdelad »

I recently read about assignment expressions introduced into Python 3.8. In Python it looks like this:
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
which would be

Code: Select all

Define n, a.s
a = "I am a test string"

If (n := Len(a)) > 10
  Debug "Length: " + Str(n)
EndIf
in PureBasic (losely translated and converted into a small example, but showing the point I'm about to make).

Basically this stores something into a variable if an expression is true. I don't know how this is realized internally and also whether it increases speed or or just for convenience.

What are your thoughts?

Sidenote: Even if this is not useful, how about extending the syntax for loops with a static parameter like

Code: Select all

For n = 1 ToStatic Len(a)
  ;Do something
Next
...not calculating Len(a) over and over because we know it won't change and obsoleting the use of another variable as temporary replacement?
PureBasic 6.11/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/150TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
STARGÅTE
Addict
Addict
Posts: 2090
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Assignment expressions

Post by STARGÅTE »

jacdelad wrote: Sat Jul 23, 2022 6:29 amBasically this stores something into a variable if an expression is true.
The assignment is anyway, because you assign first len(a) to n and then compare n with 10.

In PureBasic is can look like:

Code: Select all

Procedure.i Set(*Integer.Integer, Value.i)
	*Integer\i = Value
	ProcedureReturn Value
EndProcedure

Define n, a.s
a = "I am a test string"

If Set(@n, Len(a)) > 10
  Debug "Length: " + Str(n)
EndIf
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
jacdelad
Addict
Addict
Posts: 1492
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Assignment expressions

Post by jacdelad »

Afaik the walrus operator doesn't set the value to n, if the expression is not true. With your "Set" it is always set.
PureBasic 6.11/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/150TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Assignment expressions

Post by mk-soft »

Macro ...

Code: Select all

Define n, a.s
a = "I am a test string"

Macro IfSet(Result, Value, Assert)
  If Value Assert : Result = Value : EndIf
EndMacro

IfSet(n, Len(a), > 10)
Debug n
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
jacdelad
Addict
Addict
Posts: 1492
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Assignment expressions

Post by jacdelad »

Hm, great idea. Thanks! The only downside is that I can't combine this IfSet with other operations like "And".
PureBasic 6.11/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/150TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Assignment expressions

Post by mk-soft »

Better ... ?

Code: Select all

Define n, a.s
a = "I am a test string"
n = 5
Macro IfSet(Result, Value, Assert)
  If Not (Value Assert) : Result = Value : Else :
EndMacro

IfSet(n, Len(a), > 10)
  Debug "List is too long ({n} elements, expected <= 10)"
EndIf

Debug n
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
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Assignment expressions

Post by Rinzwind »

Won't happen. PB misses a lot of "modern" syntax conveniences (see other requests years old). Even something like inline array declaration and initialization is still not available (it is in plain old C, mind you).

Not to be sour, but realistic given existing experiences.

Initializing a structure could also be a one liner if syntax allowed or allowing procedure overloading or variable parameters or string formatting uhm format or enhanced variable scoping, etc.

Anyway, this one seems not to add too much convenience imho. ForStatic or similar is a good one though.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Assignment expressions

Post by Demivec »

I like the idea of being able to designate the ending value of a For/Next loop as a static value.

The other thought on assigning a value inside a conditional expression doesn't look to have any worth. It seems as a possible use for it is to avoid calculating a value twice, once in the conditional expression and once in the code executed after the condition is found to be true. In those situations the value of the expression should simply be assigned first and then evaluated in the conditional second.

Here is a rewrite of your example with this last thought in mind:

Code: Select all

Define n, a.s
a = "I am a test string"

n = Len(a)
If  n > 10
  Debug "Length: " + Str(n)
EndIf
I would say this is more readable and just as efficient and effective.
User avatar
jacdelad
Addict
Addict
Posts: 1492
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Assignment expressions

Post by jacdelad »

@mk-soft:

Code: Select all

IfSet(N,Len(a),>10) And b=7
Possible with Python, not possible with IfSet.
PureBasic 6.11/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/150TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
STARGÅTE
Addict
Addict
Posts: 2090
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Assignment expressions

Post by STARGÅTE »

jacdelad wrote: Sat Jul 23, 2022 7:14 am Afaik the walrus operator doesn't set the value to n, if the expression is not true. With your "Set" it is always set.
What is your reference?
https://docs.python.org/3.8/whatsnew/3. ... xpressions
There is new syntax := that assigns values to variables as part of a larger expression.
And this code:

Code: Select all

a = "Hallo"
if (n := len(a)) > 10:
    print(f"List is too long ({n} elements, expected <= 10)")
else:
    print(f"List is too short ({n} elements)")
sets n to 5:
List is too short (5 elements)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Assignment expressions

Post by mk-soft »

jacdelad wrote: Sat Jul 23, 2022 10:42 pm @mk-soft:

Code: Select all

IfSet(N,Len(a),>10) And b=7
Possible with Python, not possible with IfSet.
It's perfectly clear that this doesn't work if you know how macros work.

It's just unnecessary macros anyway.
I also think that you don't necessarily need the peculiarities of other programming languages. It also works with simple 'IF ELSE ENDIF' processing.

Code: Select all

Define n, a.s
a = "I am a test string"
n = 5
b = 7

If Len(a) > 10
  Debug "List is too long ({n} elements, expected <= 10)"
ElseIf b = 7
  n = Len(a)
EndIf

Debug n
I also think this is easier to understand than some other strange expressions in other languages.
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
jacdelad
Addict
Addict
Posts: 1492
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Assignment expressions

Post by jacdelad »

@STARGATE: You're right, I got this wrong.
@mk-soft: That's why I wanted to talk about it and get the views of other, more advanced developers.
PureBasic 6.11/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/150TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Assignment expressions

Post by Olli »

note that in ASM x86 / x64 there has been a "recent" op :

Code: Select all

cmove rax, rbx
which means here :

if a previous arithmetic op treated two similar values, then rax = rbx.

example :

Code: Select all

sub rsi, rdx
cmove rax, rbx
(x - d
if x = d
a = b)
Post Reply