record mit case

Für Fragen zur Programmiersprache auf welcher Lazarus aufbaut
Antworten
Mathias
Beiträge: 6209
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

record mit case

Beitrag von Mathias »

Ich habe da 2 fast gleiche Deklarationen.

Code: Alles auswählen

type
  TRec0 = record
    case byte of  // byte
      0: ();
      1: (a: byte);
      2: (b: word);
  end;

  TRec1 = record
    case Test: byte of  // Test: byte
      0: ();
      1: (a: byte);
      2: (b: word);
  end;
Was ist der Unterschied zwischen den beiden Deklarationen ?
Was hat dies weiter unten für einen Einfluss ?

Was macht "0: ();" für einen Sinn ?
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Benutzeravatar
theo
Beiträge: 10499
Registriert: Mo 11. Sep 2006, 19:01

Re: record mit case

Beitrag von theo »

1: Steht ja in dem Link von vorhin: https://www.freepascal.org/docs-html/ref/refsu15.html
"The optional identifier in the case statement serves to access the tag field value, which otherwise would be invisible to the programmer. It can be used to see which variant is active at a certain time. In effect, it introduces a new field in the record. "
"However, it is up to the programmer to maintain this field. "
2. Weiss auch nicht. Das scheint mir in diesem Fall nicht sinnvoll.

PascalDragon
Beiträge: 834
Registriert: Mi 3. Jun 2020, 07:18
OS, Lazarus, FPC: L 2.0.8, FPC Trunk, OS Win/Linux
CPU-Target: Aarch64 bis Z80 ;)
Wohnort: München

Re: record mit case

Beitrag von PascalDragon »

Mathias hat geschrieben:
Sa 24. Feb 2024, 14:27
Was macht "0: ();" für einen Sinn ?
Du kannst damit zeigen, dass es Varianten des Records gibt, welches keine weiteren Felder hat. Nimm zum Beispiel das Folgende an:

Code: Alles auswählen

type
  TType = (tEmpty, tLongInt, tBoolean);

  TRec = record
    case t: TType of
      tEmpty: ();
      tLongInt: (l: LongInt);
      tBoolean: (b: Boolean);
  end;
Es kommt eben immer auf den jeweiligen Usecase an, ob das sinnvoll ist.
FPC Compiler Entwickler

Antworten