Difference between revisions of "Becomes"

From Lazarus wiki
Jump to navigationJump to search
 
(22 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{Becomes}}
 
{{Becomes}}
  
<div style="float:left; margin: 0 10px 20px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">:=</div>
+
<div style="float:right; margin: 0 10px 20px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">:=</div>
  
 +
The character pair <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight>, that are a [[Colon|colon]] and an [[Equal|equal sign]] back to back, is pronounced as “becomes” and used by [[Pascal]] as the assignment operator.
 +
<br style="clear:both" />
  
 +
== assignment ==
 +
For valid assignments <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight> is surrounded by a ''single'' [[Variable|variable]] [[Identifier|identifier]] on the left hand (possibly doing a [[Typecast#variable typecast|variable typecast]]), and an [[expression]] evaluating to the [[Data type|data type]] the variable is declared as on the right hand.
  
The symbol ''''' := ''''' (the symbol [[Colon|colon]] followed by the symbol [[Equal|equal]] without intervening space or other character) is pronounced '''''becomes''''',and is used in [[Pascal]] to mean ''is assigned the value of'':
+
<syntaxhighlight lang="pascal" highlight="12-15">
 +
program assignmentDemo(input, output, stderr);
  
<syntaxhighlight>
+
const
A := 4 ;
+
diameter = 6;
Circle_Area := Pi * Diameter ;
+
 
Name := 'Smith' ;
+
var
C := 1000 - C div 2 ;
+
n: integer;
 +
area: real;
 +
givenName: string;
 +
 
 +
begin
 +
n := 42;
 +
area := pi() * diameter;
 +
givenName := 'Smith';
 +
n := 1000 - n div 2;
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The purpose in using two characters for assignment (instead of just one, say, the = sign) is to distinguish between assignment of value and comparison for equality.
+
Assignments to variables of a [[subrange types|subrange type]] should be handled with care.
 +
The [[Compiler|compiler]] can only yield out-of-range errors for ''[[Constant|constant]]'' expressions, i.&#8239;e. not depending on any [[runtime|run-time]] data.
 +
With [[sRangechecks|<syntaxhighlight lang="pascal" inline>{$rangeChecks}</syntaxhighlight>]] enabled a [[runtime error|run-time error]] can be generated.
 +
<syntaxhighlight lang="pascal">
 +
program assignmentRange(input, output, stderr);
  
For example, in languages other than Pascal one can write
+
type
:A = B = D ;  
+
naturalNumber = 1..high(longword);
:or
 
:A = B = D
 
depending on whether semicolons are required by that language.
 
  
The meaning of this expression is different depending on the language.  For example, in Fortran and Basic, the statement would mean to set variable A to the value of the question ''is B equal to D?''  In the C Programming Language, the statement would mean to set variables A and B ''to the value of'' D.  This type of instruction being in error is common enough in the C Programming Language it is often flagged by the compiler with a warning unless you tell the compiler that you intend to do this (multiple assignment) by putting parenthesis around the expression.  (The correct way to get a test for equality in the C Programming Language is to use  '''''A = B == D;''''' ).
+
var
 +
n: naturalNumber;
  
In Pascal, this confusion cannot happen because the statement is illegal.  One would write
+
begin
<syntaxhighlight>
+
{$rangechecks on}
A := B = D;
+
n := 1;          // is OK
 +
n := -42 + n;     // will cause RTE 201
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
Which would mean the same thing as Fortran or Basic, i.e. to assign to A the answer to the question of "are B and D equal?" 
 
  
To assign A, B and D the same value would require explicit separate assignments of the form
+
== handling of special data types ==
: A := D ; B := D ;
+
Where simple data types like [[Integer|<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>s]] and [[Char|<syntaxhighlight lang="pascal" inline>char</syntaxhighlight>acters]] are realized as <syntaxhighlight lang="asm" inline>mov</syntaxhighlight> instructions or alike, data types that require initialization and finalization such as [[Ansistring|<syntaxhighlight lang="pascal" inline>ansistring</syntaxhighlight>s]] or [[Class|<syntaxhighlight lang="pascal" inline>class</syntaxhighlight>es]] need special care.
:or
 
: B := D ; A := B ;
 
:or
 
: A := D ; B := A ;
 
  
Depending on whether there is a difference in execution or side effects (D could be a dynamic variable like a system [[Port|port]], a [[Function|function]] or a [[Property|property]] of an [[Object|object]], thus the value of the [[Identifier|identifier]] D might be different between the first and second use.)
+
The compiler will generate appropriate code ''copying'' data for following data types.
 +
* [[String|<syntaxhighlight lang="pascal" inline>string</syntaxhighlight>]]
 +
* [[Object|<syntaxhighlight lang="pascal" inline>object</syntaxhighlight>]]
 +
* [[Record|<syntaxhighlight lang="pascal" inline>record</syntaxhighlight>]]
 +
You don’t have to iterate over all components copying each element by hand as it is required in other programming languages.
 +
 
 +
== syntax justification ==
 +
The rationale of using two characters for assignment instead of just one, say the <syntaxhighlight lang="pascal" inline>=</syntaxhighlight>&nbsp;sign, is to distinguish between assigning values and comparing for equality.
 +
It’s got its roots in the field of mathematics where a single equal sign is read as an expression, but has no imperative connotation.
 +
 
 +
In comparison other languages than Pascal allow to write
 +
<syntaxhighlight lang="C">
 +
n = m = x;
 +
</syntaxhighlight>
 +
The semantics of this line of code varies among every programming language.
 +
For instance, in [[Fortran]] and in Basic this line means “Compare the values <syntaxhighlight lang="C" inline>m</syntaxhighlight>&nbsp;and <syntaxhighlight lang="C" inline>x</syntaxhighlight>, and if they are equal to each other <syntaxhighlight lang="C" inline>n</syntaxhighlight> becomes <syntaxhighlight lang="C" inline>true</syntaxhighlight>, <syntaxhighlight lang="C" inline>false</syntaxhighlight> otherwise.”
 +
In contrast to that the C&nbsp;programming language will assign <syntaxhighlight lang="C" inline>m</syntaxhighlight> the value of <syntaxhighlight lang="C" inline>x</syntaxhighlight>, and subsequently assign the <syntaxhighlight lang="C" inline>n</syntaxhighlight> the value of <syntaxhighlight lang="C" inline>m</syntaxhighlight>, so <syntaxhighlight lang="C" inline>n</syntaxhighlight> and <syntaxhighlight lang="C" inline>m</syntaxhighlight> both have the value <syntaxhighlight lang="C" inline>x</syntaxhighlight>.
 +
This sort of code has been a common source of errors, compilers nowadays even emit warnings when encountering multiple assignment in a single line.
 +
 
 +
In Pascal the code excerpt above is illegal.
 +
Non-productive statements are not allowed, i.&#8239;e. ''something'' has to be ''done''.
 +
 
 +
== index specification ==
 +
In declarations of enumerated data types, <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight> can be used to explicitly specify the ordinal value of a member.
 +
See [[Enum Type#Indices|enumeration types § “Indices”]] for details.
 +
 
 +
== see also ==
 +
* [[Single assignment|single assignment]], a compiler optimization
  
 
{{Symbols}}
 
{{Symbols}}
 
[[Category:Symbols]]
 

Latest revision as of 00:09, 4 February 2021

English (en) suomi (fi) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

:=

The character pair :=, that are a colon and an equal sign back to back, is pronounced as “becomes” and used by Pascal as the assignment operator.

assignment

For valid assignments := is surrounded by a single variable identifier on the left hand (possibly doing a variable typecast), and an expression evaluating to the data type the variable is declared as on the right hand.

program assignmentDemo(input, output, stderr);

const
	diameter = 6;

var
	n: integer;
	area: real;
	givenName: string;

begin
	n := 42;
	area := pi() * diameter;
	givenName := 'Smith';
	n := 1000 - n div 2;
end.

Assignments to variables of a subrange type should be handled with care. The compiler can only yield out-of-range errors for constant expressions, i. e. not depending on any run-time data. With {$rangeChecks} enabled a run-time error can be generated.

program assignmentRange(input, output, stderr);

type
	naturalNumber = 1..high(longword);

var
	n: naturalNumber;

begin
	{$rangechecks on}
	n := 1;           // is OK
	n := -42 + n;     // will cause RTE 201
end.

handling of special data types

Where simple data types like integers and characters are realized as mov instructions or alike, data types that require initialization and finalization such as ansistrings or classes need special care.

The compiler will generate appropriate code copying data for following data types.

You don’t have to iterate over all components copying each element by hand as it is required in other programming languages.

syntax justification

The rationale of using two characters for assignment instead of just one, say the = sign, is to distinguish between assigning values and comparing for equality. It’s got its roots in the field of mathematics where a single equal sign is read as an expression, but has no imperative connotation.

In comparison other languages than Pascal allow to write

n = m = x;

The semantics of this line of code varies among every programming language. For instance, in Fortran and in Basic this line means “Compare the values m and x, and if they are equal to each other n becomes true, false otherwise.” In contrast to that the C programming language will assign m the value of x, and subsequently assign the n the value of m, so n and m both have the value x. This sort of code has been a common source of errors, compilers nowadays even emit warnings when encountering multiple assignment in a single line.

In Pascal the code excerpt above is illegal. Non-productive statements are not allowed, i. e. something has to be done.

index specification

In declarations of enumerated data types, := can be used to explicitly specify the ordinal value of a member. See enumeration types § “Indices” for details.

see also


navigation bar: topic: Pascal symbols
single characters

+ (plus)  •  - (minus)  •  * (asterisk)  •  / (slash)
= (equal)  •  > (greater than)  •  < (less than)
. (period)  •  : (colon)  •  ; (semi colon)
^ (hat)  •  @ (at)
$ (dollar sign)  •  & (ampersand)  •  # (hash)
' (single quote)

character pairs

<> (not equal)  •  <= (less than or equal)  •  := (becomes)  •  >= (greater than or equal)

 •  >< (symmetric difference)  •  // (double slash)