Autor Beitrag
feuerwaran
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Fr 27.05.05 09:20 
Hallo Leute,

ich programmiere im Moment keine "Generics", aber interesieren würde mich das schon. In VC++ wird generische Programmierung unterstützt, in der Form von templates. Daher wollte ich nur wissen, ob es in Delphi auch so etwas möglich ist. Ich hab da folgendes geschrieben(Wird auch als Beispiel in diversen Hilfen aufegführt).

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function max(i,l:variant):variant;
begin
  if i<l then
    result:=l
  else
    result:=i;
end;


Der Compiler akzeptiert das so. Aber es kann zu Exeptions führen(siehe in der Online-Hilfe zu 'variant'). Ist es möglich, dem Compiler mitzuteilen, daß er ne Typenprüfung durchführt?

Wenn z.B. so was steht

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure test;
var i:integer;
var l:double;
var s:string;
begin
   i:=2;
   l:=4.1;
   s:=max(i,l);
end;


Der Compiler frißt das und es kommt auch 4,1 raus. Aber der Compiler soll mir ein Error rausgeben, wenn alle Datentypen nicht gleich sind. Geht das?

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 27.05.05 17:19 
Wenn Du Laufzeit meinst: Dafür gibt's die RTL Typ-Info, die von Variants intern benötigt wird, um ihre aktuellen Daten zu speichern. In der Unit Variants gibt es dazu die function VarType(const V: Variant): TVarType;. HTH.

Als Kompiler-Fehler geht das aber nicht zu realisieren.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.