Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Inkompatible Handle-Typen erstellen (https://www.delphipraxis.net/209636-inkompatible-handle-typen-erstellen.html)

archimedix 3. Jan 2022 19:53

Delphi-Version: 10.4 Sydney

Inkompatible Handle-Typen erstellen
 
Hallo zusammen,

weiß jemand, ob man Handle-Typen erstellen kann, die *nicht* zuweisungskompatibel sind?
Stichwort Typsicherheit.
Es soll also nicht möglich sein, ein Wert vom Typ 1 einer Variablen vom Typ 2 zuzuweisen.

Einfach so geht es schonmal nicht:
type
TSortHandle = THandle;
TContextHandle = THandle;

Und so leider auch nicht:
type
TSortHandle = type THandle;
TContextHandle = type THandle;

Mit Pointer, IntPtr und NativeInt das gleiche.

Der schöne Günther 3. Jan 2022 20:08

AW: Inkompatible Handle-Typen erstellen
 
Delphi-Quellcode:
program Project1;

type
  TWindowHandle = record
    var handle: THandle;
    class operator Implicit(const handle: THandle): TWindowHandle;
  end;

  TProcessHandle = record
    var handle: THandle;
    class operator Implicit(const handle: THandle): TProcessHandle;
  end;

class operator TWindowHandle.Implicit(const handle: THandle): TWindowHandle;
begin
  Result.handle := handle;
end;

class operator TProcessHandle.Implicit(const handle: THandle): TProcessHandle;
begin
  Result.handle := handle;
end;

var
  rawHandle: THandle;
  windowHandle: TWindowHandle;
  processHandle: TProcessHandle;
begin
  rawHandle := 1234;
  windowHandle := rawHandle;
  processHandle := rawHandle;
  // windowHandle := processHandle; // E2010 Incompatible types
end.

archimedix 3. Jan 2022 22:40

AW: Inkompatible Handle-Typen erstellen
 
ja, so ging es.
Elegant ist anders, aber immerhin.


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:15 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