Recent

Author Topic: assignment operator := does look like walrus  (Read 4118 times)

zamronypj

  • Full Member
  • ***
  • Posts: 133
    • Fano Framework, Free Pascal web application framework
assignment operator := does look like walrus
« on: January 29, 2020, 03:44:19 am »
I just realize that Pascal assignment operator := does look like walrus  :) . Python 3.8 gets another new assignement operator := and they call it walrus operator.

https://dev.to/renegadecoder94/the-controversy-behind-the-walrus-operator-in-python-4k4e
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: assignment operator := does look like walrus
« Reply #1 on: January 29, 2020, 09:42:46 am »
I just realize that Pascal assignment operator := does look like walrus  :) . Python 3.8 gets another new assignement operator := and they call it walrus operator.

https://dev.to/renegadecoder94/the-controversy-behind-the-walrus-operator-in-python-4k4e
Well, Python also does not use >< yet, so there is room for more funny mistakes.... :)

( >< means symmetric difference of two sets in Pascal)
« Last Edit: January 29, 2020, 09:44:49 am by Thaddy »
Specialize a type, not a var.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: assignment operator := does look like walrus
« Reply #2 on: January 31, 2020, 10:07:52 pm »
I've read the explanationa and I don't get it.  Maybe because I've never used Python? %)
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Re: assignment operator := does look like walrus
« Reply #3 on: January 31, 2020, 11:23:45 pm »
I've read the explanationa and I don't get it.
It seems to be a normal statement, but the result (assignment) can be "recycled", i.e. used in an other statement.

Actually it wouldn't need an extra operator, but be implicit, e.g. when using brackets.


Code: Pascal  [Select][+][-]
  1. z:= (y:= (x:= 5) +1);
instead of
Code: Pascal  [Select][+][-]
  1. x:= 5;
  2. y:= x + 1;
  3. z:= y;


Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: assignment operator := does look like walrus
« Reply #4 on: February 01, 2020, 04:31:49 am »
Code: Pascal  [Select][+][-]
  1. z:= (y:= (x:= 5) +1);
instead of
Code: Pascal  [Select][+][-]
  1. x:= 5;
  2. y:= x + 1;
  3. z:= y;

I calculated the count of the characters. Each of them both contains 17 characters (excluding spaces, line feed and carriage return). The multiline version (Pascal) is more readable and easier to debug. So what is the advantage of the single line version?

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Re: assignment operator := does look like walrus
« Reply #5 on: February 01, 2020, 09:25:54 am »
So what is the advantage of the single line version?
Personally I don't see any advantage. The example I gave is quite unreadable.

A 'more useful' example given in the above link is in a loop:

Code: Pascal  [Select][+][-]
  1. while (Value:= getNext) > 0 do begin
  2.   DoSomething(Value);
  3. end;
instead of
Code: Pascal  [Select][+][-]
  1. Value:= getNext;
  2. while Value > 0 do begin
  3.   DoSomething(Value);
  4.   Value:= getNext
  5. end;

(No, I don't like such a feature.)

simone

  • Hero Member
  • *****
  • Posts: 573
Re: assignment operator := does look like walrus
« Reply #6 on: February 01, 2020, 07:30:01 pm »
In Pascal language assignment is a statement, but not is an expression, i.e. does not return a result. Conversely in C language assignment is an expression that returns the value of the left operand after the assignment. So I suppose that the walrus operator of Phyton 3.8 is a feature inspired by C.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018