Difference between revisions of "Becomes"

From Free Pascal wiki
Jump to navigationJump to search
Line 48: Line 48:
  
 
== handling of special data types ==
 
== handling of special data types ==
Where simple data types like [[Integer|<syntaxhighlight lang="pascal" enclose="none">integer</syntaxhighlight>s]] and [[Char|<syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight>acters]] are realized as <syntaxhighlight lang="asm" enclose="none">mov</syntaxhighlight> instructions or alike, data types that require initialization and finalization such as [[Ansistring|<syntaxhighlight lang="pascal" enclose="none">ansistring</syntaxhighlight>s]] or [[Class|<syntaxhighlight lang="pascal" enclose="none">class</syntaxhighlight>es]] need special care.
+
Where simple data types like [[Integer|{{HL|intege}}s]] and [[Char|{{HL|char}}acters]] are realized as <syntaxhighlight lang="asm" enclose="none">mov</syntaxhighlight> instructions or alike, data types that require initialization and finalization such as [[Ansistring|<{{HL|ansistring}}s]] or [[Class|{{HL|class}}es]] need special care.
  
 
The compiler will generate appropriate code ''copying'' data for following data types.
 
The compiler will generate appropriate code ''copying'' data for following data types.
* [[String|<syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>]]
+
* [[String|{{HL|string}}]]
* [[Object|<syntaxhighlight lang="pascal" enclose="none">object</syntaxhighlight>]]
+
* [[Object|{{HL|object}}]]
* [[Record|<syntaxhighlight lang="pascal" enclose="none">record</syntaxhighlight>]]
+
* [[Record|{{HL|record}}]]
 
You don't have to iterate over all components copying each element by hand as it is required in other programming languages.
 
You don't have to iterate over all components copying each element by hand as it is required in other programming languages.
  
Line 61: Line 61:
  
 
In comparison other languages than Pascal allow to write
 
In comparison other languages than Pascal allow to write
<syntaxhighlight lang="C">
+
<p style="margin:2px;padding:2px;border: 3px dotted red;">Caution: The following is <u>illegal</u> in Pascal
n = m = x;
+
<br /> <code class="mw-highlight" dir="ltr"><span class="n">      n = m = x;</span></code></p>
</syntaxhighlight>
 
 
The semantics of this line of code varies among every programming language.
 
The semantics of this line of code varies among every programming language.
 
* in [[Fortran]] and in Basic this line means “Compare the values {{HLC|m}}&nbsp;and {{HLC|x}}, and if they are equal to each other, set {{HLC|n}} ro the value {{HLC|true}}, otherwise set it to the value {{HLC|false}}.”
 
* in [[Fortran]] and in Basic this line means “Compare the values {{HLC|m}}&nbsp;and {{HLC|x}}, and if they are equal to each other, set {{HLC|n}} ro the value {{HLC|true}}, otherwise set it to the value {{HLC|false}}.”
 
* In the C&nbsp;programming language this construct will assign {{HLC|m}} the value of {{HLC|x}}, and subsequently assign to {{HLC|n}} the value of {{HLC|m}}, so {{HLC|n}} and {{HLC|m}} both have the value of {{HLC|x}}.
 
* In the C&nbsp;programming language this construct will assign {{HLC|m}} the value of {{HLC|x}}, and subsequently assign to {{HLC|n}} the value of {{HLC|m}}, so {{HLC|n}} and {{HLC|m}} both have the value of {{HLC|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.
 
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''.
 
  
 
== see also ==
 
== see also ==

Revision as of 15:34, 16 October 2020

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 integes 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

Caution: The following is illegal in Pascal
n = m = x;

The semantics of this line of code varies among every programming language.

  • in Fortran and in Basic this line means “Compare the values m and x, and if they are equal to each other, set n ro the value true, otherwise set it to the value false.”
  • In the C programming language this construct will assign m the value of x, and subsequently assign to n the value of m, so n and m both have the value of 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.

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)