Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   "type xyz = type string" Sprachkonstrukt?!? (https://www.delphipraxis.net/152984-type-xyz-%3D-type-string-sprachkonstrukt.html)

ralfiii 15. Jul 2010 14:18

Delphi-Version: 2007

"type xyz = type string" Sprachkonstrukt?!?
 
Was ist denn das für ein seltsames Konstrukt:
Delphi-Quellcode:
type
  xyz = type string;
Was macht das zweite "type" da drin?

mkinzler 15. Jul 2010 14:23

AW: "type xyz = type string" Sprachkonstrukt?!?
 
Von woher stammt der Code?

dominikkv 15. Jul 2010 14:24

AW: "type xyz = type string" Sprachkonstrukt?!?
 
Das sagt, dass
Code:
xyz
und
Code:
string
das gleiche ist. Du kannst also das hier machen:
Delphi-Quellcode:
var
  A: string;
  B: xyz;
begin
  A := 'Hallo';
  B := 'Hallo';
end;

mkinzler 15. Jul 2010 14:25

AW: "type xyz = type string" Sprachkonstrukt?!?
 
Btw: was spricht dagegen für delphi-Code den Delphi-Tag zu verwenden? Da ihr das konsequenterweise nicht verwendet!

blackfin 15. Jul 2010 14:25

AW: "type xyz = type string" Sprachkonstrukt?!?
 
Aus den Delphi basics:

Zitat:

1.type Name = Existing type

Refers to an existing type, such as string by a new Name.

2.type Name = type Existing type

This has the same effect as above, but ensures that at run time, variables of this type are identified by their new type name, rather than the existing type name.

himitsu 15. Jul 2010 14:29

AW: "type xyz = type string" Sprachkonstrukt?!?
 
das type besagt, daß ein eigenständiger Type angelegt wird.

dieses erstellt einen Alias für den Type, aber in der RTTI bleibt es dennoch der externer Type "String".
Delphi-Quellcode:
type xyz = string;
.

hier wird in der RTTI für xyz ein eigenständiger Type angelegt.
Delphi-Quellcode:
type xyz = type string;
.

Delphi hat doch eine strenge Typenprüfung, so daß man nun "xyz" und "string" unterscheiden kann.


[add]
Delphi-Quellcode:
type
  PTypeInfo = ^TTypeInfo;
  TTypeInfo = packed record
    Kind: Byte;
    Name: ShortString;
   {TypeData: TTypeData}
  end;

type
  xyz = type string;
  abc = string;
  def = xyz;

begin
  ShowMessage(Format('string = %s'#10'xyz = %s'#10'abc = %s'#10'def = %s'#10, [
    PTypeInfo(TypeInfo(string))^.Name, PTypeInfo(TypeInfo(xyz))^.Name,
    PTypeInfo(TypeInfo(abc))^.Name, PTypeInfo(TypeInfo(def))^.Name]));

ralfiii 15. Jul 2010 14:35

AW: "type xyz = type string" Sprachkonstrukt?!?
 
Zitat:

Zitat von himitsu (Beitrag 1035518)
das type besagt, daß ein eigenständiger Type angelegt wird... RTTI...unterscheiden kann.

Danke! Jetzt kapier ich's endlich...

himitsu 15. Jul 2010 14:56

AW: "type xyz = type string" Sprachkonstrukt?!?
 
Bitte

Sowas kann man z.B. für sowas verwenden:
Delphi-Quellcode:
procedure a(x: xyz); overload;
procedure a(x: string); overload;
aber Achtung, dieses
Delphi-Quellcode:
a('dsa');
geht hier allerdings nicht, da die Konstante noch keinem Typen direkt zugeordnet ist und somit der Compiler nicht weiß, welcher "String"-Type nun zuständig wäre, aber man kann somit unterschiedliche Variablen übergeben und anders behandeln.


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:22 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz