Recent

Author Topic: Record..Type and VAR  (Read 3233 times)

coradi

  • Full Member
  • ***
  • Posts: 148
Record..Type and VAR
« on: October 15, 2018, 10:41:13 pm »
Hi,
in some books I found only the sample like.
Use Type xyz = Record
    var1 : Byte;
    var2:Integer;
end;

VAR another : xyz;

But some Books say that you can do this direct with

VAR xyz : RECORD
 var1:Byte;
var2:Integer;
end;

?!??!?
Why??!?! Why should I use TYPE instead VAR?
« Last Edit: October 15, 2018, 10:45:56 pm by coradi »
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Record..Type and VAR
« Reply #1 on: October 15, 2018, 10:46:57 pm »
Hi,
in some books I found only the sample like.
Use Type xyz = Record
    var1 : Byte;
    var2:Integer;
end;

VAR another : xyz;

But some Books say that you can do this direct with

Of course. It's standard Pascal.
Code: Pascal  [Select][+][-]
  1. var
  2.   xyz: record
  3.     var1 : Byte;
  4.     var2:Integer;
  5.   end;

But making it a type allows you, p.e., to declare easily more than one var:

Code: Pascal  [Select][+][-]
  1. type
  2.   Txyz = record
  3.     var1 : Byte;
  4.     var2:Integer;
  5.   end;
  6.  
  7. var
  8.   a, b, c: Txyz;

and also to use Txyz (almost) as if it were a built-in type, i.e. pass it as function/procedure parameters, use it as function/parameter result, etc.
« Last Edit: October 15, 2018, 11:23:41 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Record..Type and VAR
« Reply #2 on: October 15, 2018, 11:05:58 pm »
lucamar did not draw attention to it, but note how he changed your type name to Txyz.

You can ignore this convention, but once you start writing complex code, you'll find that visually identifying all types (as distinct from other identifiers) by a leading "T" makes code far more readable for others as well as you.

A further advantage of explicitly named types rather than anonymous types is that it becomes straightforward to use the variable in a function/procedure call:
Code: Pascal  [Select][+][-]
  1. procedure ReportXYZ(anXYZ: Txyz);
  2. begin
  3.    WriteLn('var1: ', anXYZ.var1, ', var2:', anXYZ.var2);
  4. end;
Without the type you can't write
Code: Pascal  [Select][+][-]
  1. var
  2.    xyz: TXYZ;
  3. begin   ...
  4.   ReportXYZ(xyz);
  5.  ...
  6. end.

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: Record..Type and VAR
« Reply #3 on: October 15, 2018, 11:24:49 pm »
There is another advantage of using a separate type declaration. You can easily assign two records, whereas this example does not compile:
Code: Pascal  [Select][+][-]
  1. var
  2.   xyz: record
  3.         var1 : Byte;
  4.         var2 : Integer;
  5.       end;
  6.   abc: record
  7.         var1 : Byte;
  8.         var2 : Integer;
  9.       end;
  10. begin
  11.   abc.var1 := 1;
  12.   abc.var2 := -2;
  13.   xyz := abc;
  14. end.
You get the somewhat cryptic error message: Error: Incompatible types: got "" expected ""

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Record..Type and VAR
« Reply #4 on: October 15, 2018, 11:42:26 pm »
The error message is perhaps confusing to beginners.
However, it makes perfect sense from the point of view of the compiler.
The compiler does not know that the two distinct (internal) types it created for abc and xyz are assignment-compatible.
It reports this assumed incompatibility as best it can, using the blank anonymous names the two types have.

coradi

  • Full Member
  • ***
  • Posts: 148
Re: Record..Type and VAR
« Reply #5 on: October 16, 2018, 11:28:49 pm »
I'm not shure, If I understand correct..but it doesn't work
It says that are incompatible types if I try it as Type, expected abc, got xyz...
 can't copy and paste it from Dos Window :-(
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Record..Type and VAR
« Reply #6 on: October 16, 2018, 11:56:42 pm »
I'm not shure, If I understand correct..but it doesn't work
It says that are incompatible types if I try it as Type, expected abc, got xyz...

GammaTester's code is, as he says, expected to fail. His point was that using a type it does work:
Code: Pascal  [Select][+][-]
  1. type
  2.   Txyz = record
  3.     var1 : Byte;
  4.     var2 : Integer;
  5.   end;
  6.  
  7. var
  8.   xyz, abc: Txyz;
  9. begin
  10.   abc.var1 := 1;
  11.   abc.var2 := -2;
  12.   xyz := abc;
  13.   writeln('xyz.var1 = ', xyz.var1,', xyz.var2 = ', xyz.var2);
  14. end.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018