Page 1 of 1
Assignment expressions
Posted: Sat Jul 23, 2022 6:29 am
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?
Re: Assignment expressions
Posted: Sat Jul 23, 2022 6:54 am
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
Re: Assignment expressions
Posted: Sat Jul 23, 2022 7:14 am
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.
Re: Assignment expressions
Posted: Sat Jul 23, 2022 12:00 pm
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
Re: Assignment expressions
Posted: Sat Jul 23, 2022 12:07 pm
by jacdelad
Hm, great idea. Thanks! The only downside is that I can't combine this IfSet with other operations like "And".
Re: Assignment expressions
Posted: Sat Jul 23, 2022 12:17 pm
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
Re: Assignment expressions
Posted: Sat Jul 23, 2022 6:48 pm
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.
Re: Assignment expressions
Posted: Sat Jul 23, 2022 7:53 pm
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.
Re: Assignment expressions
Posted: Sat Jul 23, 2022 10:42 pm
by jacdelad
@mk-soft:
Possible with Python, not possible with IfSet.
Re: Assignment expressions
Posted: Sun Jul 24, 2022 12:05 am
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)
Re: Assignment expressions
Posted: Sun Jul 24, 2022 1:10 am
by mk-soft
jacdelad wrote: Sat Jul 23, 2022 10:42 pm
@mk-soft:
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.
Re: Assignment expressions
Posted: Sun Jul 24, 2022 8:09 am
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.
Re: Assignment expressions
Posted: Sun Jul 24, 2022 11:55 pm
by Olli
note that in ASM x86 / x64 there has been a "recent" op :
which means here :
if a previous arithmetic op treated two similar values, then rax = rbx.
example :
(x - d
if x = d
a = b)